How to write cell array into a csv file
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (8)
Jon
el 5 de Abr. de 2013
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 comentarios
Cedric
el 5 de Abr. de 2013
+1 ;-)
Jalaj Bidwai
el 6 de Abr. de 2013
Jon
el 8 de Abr. de 2013
You could generalize it for an arbitrary number of columns as follows:
c = {'abc' 'def' 'ghk';[23],[24],[67];[87],[13],[999];[656],[6767],[546]};
fid = fopen('junk.csv','w')
numColumns = size(c,2);
% use repmat to construct repeating formats
% ( numColumns-1 because no comma on last one)
headerFmt = repmat('%s,',1,numColumns-1);
numFmt = repmat('%f,',1,numColumns-1);
fprintf(fid,[headerFmt,'%s\n'],c{1,:})
fprintf(fid,[numFmt,'%f\n'],c{2:end,:})
fclose(fid)
Even better, if you have the statistics toolbox, you can use a dataset array and do the whole thing very cleanly as:
ds = cell2dataset(c);
export(ds,'file','junk2.csv','delimiter',',')
Cedric
el 8 de Abr. de 2013
I don't know if the OP will read your comment, but thank you for the information; I didn't know EXPORT.
Jalaj Bidwai
el 8 de Abr. de 2013
Harsha Vardhana Padullaparti
el 16 de Jun. de 2016
Jonathan, Perfect! Thanks so much for your input.
Vibhav Gaur
el 21 de Jun. de 2021
writecell(c, 'c.csv')
Azzi Abdelmalek
el 5 de Abr. de 2013
Editada: Azzi Abdelmalek
el 5 de Abr. de 2013
1 voto
2 comentarios
Jon
el 5 de Abr. de 2013
csvwrite only works on numeric arrays, not cell arrays with strings and numbers
Youcef Yahiaoui
el 19 de Sept. de 2015
Azzi,good to see your comments here. I usually use fprintf(fid...) for the first header lines then use csvwrite with the option -append...
Zumbo_123
el 7 de En. de 2016
0 votos
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.
Peter Farkas
el 9 de Oct. de 2017
0 votos
Convert to table and use writetable function T = cell2table(c(2:end, :)); T.Properties.VariableNames = c(1:end, :); writetable(T, res.csv);
Aaron Thrasher
el 20 de Abr. de 2018
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 comentarios
Aaron Thrasher
el 20 de Abr. de 2018
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
el 19 de Oct. de 2019
Thanks for sharing your work!
Yoram Segal
el 27 de Ag. de 2018
0 votos
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)
TripleSSSS
el 4 de Abr. de 2019
0 votos
Categorías
Más información sobre Text Data Preparation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!