在Matlab中将单元格数组另存为.xlsx或.csv文件 [英] save a cell array in matlab as .xlsx or .csv file
问题描述
我有一个单元格数组 myFile 637x16.单元格数组的第一行由字符串组成,因为它们将是.xlsx/.csv文件中列的标签.
I have a cell array myFile 637x16. The first row of the cell array is made of strings, because they will be the columns' labels in the .xlsx/.csv file.
从第二行开始,单元格数组由一些带有字符串的列和一些带有数字的列组成. 我想将此单元格数组导出为.xlsx或.csv文件.
From the second row on, the cell array is made of some columns with strings, and some columns with numbers. I would like to export this cell array as a .xlsx or .csv file.
这是我所拥有的一个例子:
Here is an example of what I have:
'subject' 'PeakA' 'PeakL' 'number' 'epoch' 'code' 'type' 'latency' 'nitem' 'condition' 'ia' 'cover' 'variety' 'init_index' 'init_time' 'urevent'
5 3.50 82 13 1 201011 'pre' 2502 201 1 1 'y' 'h' 13 13.92 13
5 -1.27 112 55 2 61011 'pre' 8213 61 1 1 'y' 'h' 55 53.90 55
5 6.59 99 85 3 124011 'pre' 13924 124 1 1 'y' 'h' 85 82.45 85
5 12.65 105 127 4 178011 'pre' 19635 178 1 1 'y' 'h' 127 122.43 127
5 -0.35 105 157 5 89011 'pre' 25346 89 1 1 'y' 'h' 157 150.98 157
5 10.29 93 163 6 132011 'pre' 31057 132 1 1 'y' 'h' 163 156.69 163
5 4.61 65 193 7 166011 'pre' 36768 166 1 1 'y' 'h' 193 185.25 193
5 1.45 51 199 8 212011 'pre' 42479 212 1 1 'y' 'h' 199 190.96 199
我尝试过:
xlswrite('filename.xlsx', myFile);
但是它给了我这个错误:
but it gives me this error:
Warning: Could not start Excel server for export.
XLSWRITE will attempt to write file in CSV format.
> In xlswrite (line 174)
Error using xlswrite (line 187)
An error occurred on data export in CSV format.
Caused by:
Error using dlmwrite (line 112)
The input cell array cannot be converted to a matrix.
推荐答案
如果您有足够最新的Matlab版本(R2013b或更旧的版本),则writetable
是您的朋友.
If you have a sufficiently recent version of Matlab (R2013b or older), writetable
is your friend.
%# create a table
tbl = cell2table(yourCellArray(2:end,:),'variableNames',yourCellArray(1,:));
%# write to file
writetable(tbl,'filename.xlsx')
如果要使用xlswrite
,可能值得先将所有数据转换为字符串,或者在写入其余数据之前分别写入变量名称-我相信Matlab会在第一行检查数据类型,这可以导致类型转换错误.
If you want to use xlswrite
, it may be worth converting all data to string first, or to write the variable names separately, before you write the rest - I believe Matlab checks data types on the first row, which can cause typecast errors.
这篇关于在Matlab中将单元格数组另存为.xlsx或.csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!