I have 14 arrays that i would like to export to a single excel file. All arrays are of the size 1x5000. I'd like the excel file to be structured like this:
variable 1 value1 value2 value3 ..... value n
variable2 value1 value2 value3 ..... valuen
What i have now is this:
t = table(time, area, volume, radii1, radii2, centre1, centre2);
t = rows2vars(splitvars(t));
writetable(t, append(save_name,'.xlsx'))
The excell file looks like this:
time1 val1
time2 val2
time3 val3
.
.
.
.
timen valn
And so on for all the arrays.
Does anybody have any tips?

 採用された回答

Jeremy Hughes
Jeremy Hughes 2022 年 4 月 10 日

0 投票

If each variable is 1x5000 then your original code is creating a 1x35000 block of data when you put it in the table. This is valid since a table can have multi-column variables.
What you want to do is flip the vectors to 5000x1, giving you a 5000x7 table, then rows2vars will make that 7x5000.
t = table(time', area', volume', radii1', radii2', centre1', centre2');
t = rows2vars(t);
writetable(t, append(save_name,'.xlsx'))
Another way to do this is to write each as a row in a large matrix. (Assuming each is a double)
writematrix([time; area; volume; radii1; radii2; centre1; centre2], filename)
Or if time is datetime, or duration
writematrix(time, filename)
writematrix([area; volume; radii1; radii2; centre1; centre2], filename,"Writemode","Append")

3 件のコメント

KuriGohan
KuriGohan 2022 年 4 月 11 日
Thank you very much. It works however all the variable names are changed to var1 var2 var3,... is there a way to keep the original variable name when writting to excel?
Walter Roberson
Walter Roberson 2022 年 4 月 11 日
編集済み: Walter Roberson 2022 年 4 月 11 日
t = table(time', area', volume', radii1', radii2', centre1', centre2', 'VariableNames', {'time', 'area', 'volume', 'radii1', 'radii2', 'center1', 'center2'});
writetable(t, filename)
KuriGohan
KuriGohan 2022 年 4 月 11 日
Thank you very much.

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

その他の回答 (0 件)

製品

リリース

R2020b

タグ

質問済み:

2022 年 4 月 10 日

コメント済み:

2022 年 4 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by