MATLAB Answers

How to write cell array into a csv file

2,250 ビュー (過去 30 日間)
Jalaj Bidwai
Jalaj Bidwai 2013 年 4 月 5 日
コメント済み: Michael Barrow 2019 年 10 月 19 日
Hello Everyone, I have a cell array C where the first row is string and the remaining rows contain numbers. How do I write the variable C into a CSV file?
For example,
c = {'abc' 'def' 'ghk';[23],[24],[67];[87],[13],[999];[656],[6767],[546]};
Thanks

  0 件のコメント

サインイン to comment.

採用された回答

Cedric Wannaz
Cedric Wannaz 2013 年 4 月 5 日
編集済み: MathWorks Support Team 2018 年 11 月 27 日
To write the data in C to a CSV file. Use “writetable” in combination with the “cell2table” function.
% Convert cell to a table and use first row as variable names
T = cell2table(c(2:end,:),'VariableNames',c(1,:))
% Write the table to a CSV file
writetable(T,'myDataFile.csv')
For more information see:

  5 件のコメント

表示 2 件の古いコメント
Jalaj Bidwai
Jalaj Bidwai 2013 年 4 月 6 日
Thanks guys..for replying and giving the solution..Thankyou very much
Image Analyst
Image Analyst 2015 年 10 月 19 日
Sara Khalifa's flag moved here:
Great Solution. Thanks!
Bidisha Das
Bidisha Das 2018 年 8 月 29 日
What if I want the csv file in append mode?

サインイン to comment.

その他の回答 (7 件)

Jon
Jon 2013 年 4 月 5 日
You could do it as follows with fprintf
c = {'abc' 'def' 'ghk';[23],[24],[67];[87],[13],[999];[656],[6767],[546]};
fid = fopen('junk.csv','w')
fprintf(fid,'%s, %s, %s\n',c{1,:})
fprintf(fid,'%f, %f, %f\n',c{2:end,:})
fclose(fid)

  6 件のコメント

表示 3 件の古いコメント
Cedric Wannaz
Cedric Wannaz 2013 年 4 月 8 日
I don't know if the OP will read your comment, but thank you for the information; I didn't know EXPORT.
Jalaj Bidwai
Jalaj Bidwai 2013 年 4 月 8 日
Jonathan,Fanstastic. Both of the ways have worked for me..Thank you for the help..I really aprreciate it...
Harsha Vardhana Padullaparti
Harsha Vardhana Padullaparti 2016 年 6 月 16 日
Jonathan, Perfect! Thanks so much for your input.

サインイン to comment.


Azzi Abdelmalek
Azzi Abdelmalek 2013 年 4 月 5 日
編集済み: Azzi Abdelmalek 2013 年 4 月 5 日
use csvwrite functionn

  2 件のコメント

Jon
Jon 2013 年 4 月 5 日
csvwrite only works on numeric arrays, not cell arrays with strings and numbers
Youcef Yahiaoui
Youcef Yahiaoui 2015 年 9 月 19 日
Azzi,good to see your comments here. I usually use fprintf(fid...) for the first header lines then use csvwrite with the option -append...

サインイン to comment.


Zumbo_123
Zumbo_123 2016 年 1 月 7 日
Alternatively, you could use the method developed by Sylvain Fiedler. Below is the link to the file. It works with empty cells, numeric, char, string and logical cells. One array can contain all of them, but only one value per cell.

  0 件のコメント

サインイン to comment.


Peter Farkas
Peter Farkas 2017 年 10 月 9 日
Convert to table and use writetable function T = cell2table(c(2:end, :)); T.Properties.VariableNames = c(1:end, :); writetable(T, res.csv);

  0 件のコメント

サインイン to comment.


Aaron Thrasher
Aaron Thrasher 2018 年 4 月 20 日

I came to this page looking for an answer and found that all solutions were very slow for large arrays that include numeric and char based cell arrays of non-standard combinations. Instead, I created my own based off of previous comments and other research. I hope this helps someone as it has helped me reduce the time significantly.

function cellWrite(filename,origCell)
% save a new version of the cell for reference
modCell = origCell;
% assume some cells are numeric, in which case set to char
iNum = cellfun(@isnumeric,origCell);
%% Method 1 using indexing and straight conversion = 7 sec
tic
modCell(iNum) = cellstr(num2str([origCell{iNum}]'));
% toc
%% Method 2 using arrayfun = 25 sec
% tic
% modCell(iNum) = arrayfun(@num2str,[origCell{iNum}],'unif',0);
% toc
% tic
[rNum,cNum] = size(origCell);
frmt = repmat([repmat('%s,',1,cNum-1),'%s\n'],1,rNum);
fid = fopen(filename,'w');
fprintf(fid,frmt,modCell{:});
fclose(fid);
toc
end

  2 件のコメント

Aaron Thrasher
Aaron Thrasher 2018 年 4 月 20 日
Correction to the code - the cell needs to be transposed before writing because the data is written left to right vs. matlab reading cell top down.
Michael Barrow
Michael Barrow 2019 年 10 月 19 日
Thanks for sharing your work!

サインイン to comment.


Yoram Segal
Yoram Segal 2018 年 8 月 27 日
convert the cell to a table and save the table as CSV
TT = array2table(C,'VariableNames',{'abc' 'def' 'ghk'});
writetable(TT,filename);
To read it back you can use TT = readtable(filename)

  0 件のコメント

サインイン to comment.


TripleSSSS
TripleSSSS 2019 年 4 月 4 日
Matlab now support write from cell array to file
please check: writecell

  0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。


Translated by