Info

この質問は閉じられています。 編集または回答するには再度開いてください。

I need help, I have xlswrite row problems

1 回表示 (過去 30 日間)
Matthew Covington
Matthew Covington 2019 年 4 月 15 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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

回答 (1 件)

Geoff Hayes
Geoff Hayes 2019 年 4 月 16 日
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 件のコメント
Geoff Hayes
Geoff Hayes 2019 年 4 月 16 日
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?
Matthew Covington
Matthew Covington 2019 年 4 月 16 日
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

Community Treasure Hunt

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

Start Hunting!

Translated by