How do I put the text frim the excel file into the fprintf statement without using their actual names

1 回表示 (過去 30 日間)
You must read in data from two, separate, Excel files. The first file will contain a row of names and is henceforth referred to as the Names File. The second file will contain numeric data and is henceforth referred to as the Scores File. The Scores File will have the same number of columns as the Names File, and a total of 13 rows of numeric data for each column. For instance, if there are two names in the names File, then the Scores File will contain 13 rows by 2 columns of scoring data. Each column in the Scores File represents the 13 weekly scores for the player with the name in the same column in the Names File. Where the A column in the Scores file represents the weekly scores for John. Your job is to write MATLAB to read in the data from the two Excel files, then determine the winner of each week (the player with the highest score), and the winner of the league (the highest score accumulated through all 13 weeks). You will then display to the Command Window, the winner of each week using their names and scores.
So far I have this code:
clc,clear
scores = xlsread('C:\Users\persephone\Desktop\ScoresFile.xlsx'); [num txt raw] = xlsread('C:\Users\persephone\Desktop\NamesFile.xlsx');
plot(scores,'o-') xlabel('Week Number') ylabel('Weekly Score') title('Fantasy Football Scores')
M = max(scores,[],2);
S = sum(scores) W = max(S)
fprintf('The winner for week 1 is name with %d points.\n',M(1)) fprintf('The winner for week 2 is name with %d points.\n',M(2)) fprintf('The winner for week 3 is name with %d points.\n',M(3)) fprintf('The winner for week 4 is name with %d points.\n',M(4)) fprintf('The winner for week 5 is name with %d points.\n',M(5)) fprintf('The winner for week 6 is name with %d points.\n',M(6)) fprintf('The winner for week 7 is name with %d points.\n',M(7)) fprintf('The winner for week 8 is name with %d points.\n',M(8)) fprintf('The winner for week 9 is name with %d points.\n',M(9)) fprintf('The winner for week 10 is name with %d points.\n',M(10)) fprintf('The winner for week 11 is name with %d points.\n',M(11)) fprintf('The winner for week 12 is name with %d points.\n',M(12)) fprintf('The winner for week 13 is name with %d points.\n',M(13)) fprintf('The overall winner is name with %d points.\n',W)
where it says name in the fprintf statements, I need to figure out how to make it say the winner's name.
  1 件のコメント
dpb
dpb 2017 年 5 月 5 日
Format the code...select and use the {}Code button (OH! how I wish TMW would make this fixed-format field by default! What a lot of effort we could save.)

サインインしてコメントする。

採用された回答

Matthew Eicholtz
Matthew Eicholtz 2017 年 5 月 5 日
This may not entirely answer your question, but hopefully it points you in the right direction. Suppose you have a vector of weeks, names, and scores:
weeks = 1:5;
names = {'Tom Brady','Tom Brady','Tom Brady','Matt Ryan','Tom Brady'};
scores = [17 45 28 66 49];
You can concatenate these into a cell array:
c = cat(1,num2cell(weeks),names,num2cell(scores));
And then use a single fprintf statement:
fprintf('The winner of week %d is %s with %d points.\n',c{:});
The output should look like this:
The winner of week 1 is Tom Brady with 17 points.
The winner of week 2 is Tom Brady with 45 points.
The winner of week 3 is Tom Brady with 28 points.
The winner of week 4 is Matt Ryan with 66 points.
The winner of week 5 is Tom Brady with 49 points.
Does that help?
  7 件のコメント
Persephone Powell
Persephone Powell 2017 年 5 月 6 日
clc,clear
scores = xlsread('C:\Users\persephone\Desktop\ScoresFile.xlsx');
[num txt raw] = xlsread('C:\Users\persephone\Desktop\NamesFile.xlsx');
plot(scores,'o-')
xlabel('Week Number')
ylabel('Weekly Score')
title('Fantasy Football Scores')
M = max(scores,[],2);
S = sum(scores)
W = max(S)
fprintf('The winner for week 1 is name with %d points.\n',M(1))
fprintf('The winner for week 2 is name with %d points.\n',M(2))
fprintf('The winner for week 3 is name with %d points.\n',M(3))
fprintf('The winner for week 4 is name with %d points.\n',M(4))
fprintf('The winner for week 5 is name with %d points.\n',M(5))
fprintf('The winner for week 6 is name with %d points.\n',M(6))
fprintf('The winner for week 7 is name with %d points.\n',M(7))
fprintf('The winner for week 8 is name with %d points.\n',M(8))
fprintf('The winner for week 9 is name with %d points.\n',M(9))
fprintf('The winner for week 10 is name with %d points.\n',M(10))
fprintf('The winner for week 11 is name with %d points.\n',M(11))
fprintf('The winner for week 12 is name with %d points.\n',M(12))
fprintf('The winner for week 13 is name with %d points.\n',M(13))
fprintf('The overall winner is name with %d points.\n',W)
Guillaume
Guillaume 2017 年 5 月 6 日
Egad! After how many times writing the same line do you consider using a loop instead?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by