xlswrite in case of vectors

寵の児 提交于 2019-12-04 05:49:10

问题


I have a .mat file which contains titles={'time','data'} and 2 column vectors: time=[1;2;3;4;5] and data=[10;20;30;40;50].

I created a new cell called table={'time','data';time data} and i used:

xlswrite(filename,table);

However, when i open the xlsx file it shows me only the titles and not showing the numbers. I saw that xlswrite will show empty cell in case im trying to export more than 1 number in a cell.

Is there anything i can do to export the whole vector instead of writing each value in it's cell?

The final result that i tried to get is like this:

time   data
1       10
2       20
3       30
4       40
5       50

回答1:


You have a couple options. Usually what I do is break it into two xlswrite calls, one for the header and one for the data.

titles = {'time','data'};
time = [1;2;3;4;5];
data = [10;20;30;40;50];

xlswrite('myfile.xlsx', titles, 'Sheet1', 'A1');
xlswrite('myfile.xlsx', [time, data], 'Sheet1', 'A2');

Alternatively, if you have R2013b or newer you can also use the table builtin, which has its own method for writing out data. With the same sample data:

mytable = table(time, data, 'VariableNames', titles);
writetable(mytable, 'myfile.xlsx');


来源:https://stackoverflow.com/questions/31028741/xlswrite-in-case-of-vectors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!