将 matlab 中的元胞数组保存为 .xlsx 或 .csv 文件

save a cell array in matlab as .xlsx or .csv file

我有一个元胞数组 myFile 637x16。元胞数组的第一行由字符串组成,因为它们将是 .xlsx/.csv 文件中的列标签。

从第二行开始,元胞数组由一些包含字符串的列和一些包含数字的列组成。 我想将此元胞数组导出为 .xlsx 或 .csv 文件。

这是我的例子:

'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);

但它给了我这个错误:

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 是您的朋友。

%# create a table
tbl = cell2table(yourCellArray(2:end,:),'variableNames',yourCellArray(1,:));

%# write to file
writetable(tbl,'filename.xlsx')

如果你想使用 xlswrite,可能值得先将所有数据转换为字符串,或者在你编写其余部分之前单独编写变量名 - 我相信 Matlab 首先检查数据类型行,这可能会导致类型转换错误。