フィルターのクリア

Using writetable in a loop to save each iteration as a new line

14 ビュー (過去 30 日間)
Eric Jenkins
Eric Jenkins 2017 年 11 月 3 日
コメント済み: William Harris 2021 年 9 月 30 日
I am trying to use writetable in a loop so that each iteration prints on a new line. Since the data is only one row, I would want something like A1, A2, A3... but it seems like this can't be done because 'A1' is a string. This is what I have:
for w = 1:n %each folder has a different number of files defined by n
T(1:1,:)
writetable(T,filename, 'Sheet',1,'Range', 'A(w)') %this needs to be 'A1' to assign the location of the table, but is there a way to use A(index)?
end
Thanks a bunch

採用された回答

Chenchal
Chenchal 2017 年 11 月 3 日
use ['A' num2str(w)] where w = 1 or a loop var
% code
writetable(T,filename,'Sheet',1,'Range', ['A' num2str(w)])
  2 件のコメント
Eric Jenkins
Eric Jenkins 2017 年 11 月 3 日
Thank you so much, this worked great. I did run into the issue of the table heading messing up the output when I write to an xls file, because as the file was incremented per row, the heading was taking up the allocated row. But I did a quick fix using:
'WriteVariableNames', false
I am not sure if there is a better way to do this where I could keep only the first row of headings?
William Harris
William Harris 2021 年 9 月 30 日
To keep the headings you can use an if clause:
init_cell = sprintf( 'A%s',num2str(count) );
if count == 1
writetable(T, filename, 'Sheet', 1, 'Range', init_cell);
else
writetable(T, filename, 'Sheet', 1, 'Range', init_cell, 'WriteVariableNames', false);
end
However, I am getting an issue with this where the final table doesn't include the first row of data. Does anyone have a fix for this?

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by