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
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
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 个评论
Guillaume
2017-5-6
Egad! After how many times writing the same line do you consider using a loop instead?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!