MATLAB Answers

0

I need help, I have xlswrite row problems

Asked by Matthew Covington on 15 Apr 2019
Latest activity Commented on by Matthew Covington on 16 Apr 2019
The first picture (excel.PNG) is what my data looks like using xlswrite, the second picture (instructions.PNG) is what it is supposed to look like. Here is the code I used to display it, I know I am not displaying it correctly to the correct cells because I am using n as the cell number. There are 32 games and I am only supposed to display home games so the number of the game doesn't correspond with the number of the cell I am supposed to put it in. Also the data, such as the numbers, are wrong due to using the n as the cell number as well.
% data
home = 'vs';
for n = 1:sRows
if strcmp(home, headers{n, 1}) == 1
date = [stats(n, 1), stats(n, 2), stats(n, 3)];
opponent = headers{n, 2};
pointsAU = stats(n, 4);
pointsOpp = stats(n, 5);
attend = stats(n, 6);
cellDate = sprintf('A%d', n);
cellOpp = sprintf('B%d', n);
cellPointsAU = sprintf('C%d', n);
cellPointsOpp = sprintf('D%d', n);
cellAttend = sprintf('E%d', n);
OPPONENT = sprintf('%s', opponent);
DATE = sprintf('%d/%d/%d', date);
xlswrite(FILENAME, {DATE}, OUTSPREADSHEET, cellDate)
xlswrite(FILENAME, {OPPONENT}, OUTSPREADSHEET, cellOpp)
xlswrite(FILENAME, pointsAU, OUTSPREADSHEET, cellPointsAU)
xlswrite(FILENAME, pointsOpp, OUTSPREADSHEET, cellPointsOpp)
xlswrite(FILENAME, attend, OUTSPREADSHEET, cellAttend)
end
end

  0 Comments

Sign in to comment.

1 Answer

Answer by Geoff Hayes
on 16 Apr 2019

Matthew - the row problem can be handled by using a different variable (other than n) to determine where the next row should be. Something like
% data
home = 'vs';
insertRowAt = 1;
for n = 1:sRows
if strcmp(home, headers{n, 1}) == 1
% your code
% code to insert data at cell
cellDate = sprintf('A%d', insertRowAt);
cellOpp = sprintf('B%d', insertRowAt);
cellPointsAU = sprintf('C%d', insertRowAt);
cellPointsOpp = sprintf('D%d', insertRowAt);
cellAttend = sprintf('E%d', insertRowAt);
insertRowAt = insertRowAt + 1;
% your other code
end
end
The above might fix the problem with all of the blank lines in your output. However, for the other problem of not showing the correct game... I don't understand how the
if strcmp(home, headers{n, 1}) == 1
condition is supposed to work since home is initialized to 'vs'. What is headers{n, 1} as a string? Why are you comparing this to 'vs' and assuming that this is a home game?

  3 Comments

The away games have an 'at' in this column, the home games a 'vs' and neutral games a 'n'. I only want the rows of the home games to be written into the excel spreadsheet.
so how is the incorrect game data being shown then? are you sure that the data in headers and stats correspond correctly between the rows?
Thats what happened, (n-2) in the place of n for all except the opponent worked becasue they use stats. Thanks for all the help

Sign in to comment.