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

2 views (last 30 days)
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:
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 Comment
dpb on 5 May 2017
Format the 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.)

Sign in to comment.

Accepted Answer

Matthew Eicholtz
Matthew Eicholtz on 5 May 2017
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?

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!

Translated by