2015-11-23 102 views
2

我有一個單元格陣列myFile 637x16。單元格數組的第一行由字符串組成,因爲它們將成爲.xlsx/.csv文件中列的標籤。將matlab中的單元格aray保存爲.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. 
+0

改爲使用'fprintf'。 ''fprintf(fid,'%s,%f \ n',data {k,:});'''for循環'用'k'迭代器循環數據。 – NKN

+0

不是答案,但考慮使用[table](http://www.mathworks.com/help/matlab/ref/writetable.html)數據類型,而不是此單元格數組 – Dan

+0

@Dan,如果我運行myTable = cell2table(MYFILE);它將返回以下警告消息超過100次: '警告:在轉換爲字符期間,超出範圍或非整數值被截斷。 >在matlab.internal.table.container2vars(第57行)中' '在cell2table中(第34行)警告:超出範圍或非整數值在轉換爲字符期間被截斷。' – dede

回答

0

Matlab版本(R2013b或更舊),writetable是你的朋友。

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

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

如果你想使用xlswrite,它可能是值得所有的數據轉換爲字符串第一,還是要分開來寫的變量名,你寫的其他部分之前 - 我相信Matlab的第一行上檢查數據類型,這可能會導致類型錯誤。

+0

感謝@Jonas的回答。有效! :) – dede

相關問題