File Exchange

image thumbnail

Cell Array to CSV-file [improved cell2csv.m]

version 1.6.0.0 (2.76 KB) by Jerry
Writes cell array content into a *.csv file.

10 Downloads

Updated 10 Jul 2014

View Version History

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

now works with empty cells, numeric, char, string, row vector, and logical cells.
row vector such as [1 2 3] will be separated by two spaces, that is "1 2 3"
other types will be converted to the string "NA".
One array can contain all of them, but only one value per cell.
2x times faster than Sylvain's codes (8.8s vs. 17.2s):
tic;C={'te','tm';5,[1,2];true,{}};C=repmat(C,[10000,1]);cell2csv([datestr(now,'MMSS') '.csv'],C);toc;

credits go to Sylvain Fiedler:
modified and optimized from Sylvain Fiedler's codes at http://www.mathworks.com/matlabcentral/fileexchange/4400-cell-array-to-csv-file--cell2csv-m-

To get csv back to cell, use csv2cell(). There are a couple of that on fileexchange. I have packed them into a single library together with other useful routines. Feel free to check at http://www.mathworks.com/matlabcentral/fileexchange/47193-easy-matlab-and-shell-interaction-library--ez-m-

Cite As

Jerry (2021). Cell Array to CSV-file [improved cell2csv.m] (https://www.mathworks.com/matlabcentral/fileexchange/47055-cell-array-to-csv-file-improved-cell2csv-m), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (13)

Louise Wilson

Oli Fairfax

Great code. I made a small modification to allow me to copy the contents to the clipboard too if strcmp(fileName, 'clipboard') using Dec's answer here: https://stackoverflow.com/questions/13922929/pasting-a-cell-array-of-strings-into-the-clipboard

AKASH YADAV

Thanks.. Easy to implement

Georgia Peterson

Is is possible to write the string below to a single cell using this function? It separates it at the comma into two cells.

str1 = {sprintf( 'LINESTRING(%.2f %.2f, %.2f %.2f) '

Philipp Schneider

Add this line (47) to replace escape characters (%):

cellArray = regexprep(cellArray,'(?<!%)%(?!%)','%%');

else fprintf will escape all '%' signs

jasongrig

It does not work when the string in the cell has been created using strcat and not [ ].

Thomas Lew

Same problem, even when executing
tic;C={'te','tm';5,[1,2];true,{}};C=repmat(C,[10000,1]);cell2csv([datestr(now,'MMSS') '.csv'],C);toc;
as mentioned above

Ryszard Cetnarski

Same problem as Sandro

Sandro Balestrino

> C = num2cell(magic(5));
disp(C)
[17] [24] [ 1] [ 8] [15]
[23] [ 5] [ 7] [14] [16]
[ 4] [ 6] [13] [20] [22]
[10] [12] [19] [21] [ 3]
[11] [18] [25] [ 2] [ 9]

>> mkdir Test1
>> cell2csv('.\Test1\cell2csv.csv',C,',')
??? Undefined function or method 'isrow' for input arguments of type 'double'.

Error in ==> cell2csv>StringX at 63
elseif isnumeric(x) && isrow(x)

Error in ==> cell2csv at 46
cellArray = cellfun(@StringX, cellArray, 'UniformOutput', false);

...guys, where am I going wrong...?

Arturo Moncada-Torres

Harm

Very nice!
However, strings with \, / and % characters go wrong. This can be fixed with a strrep into \\, // and %% respectively. These statements can be included in the subfunction stringX in cell2csv.

David

Whew! Easy solution to a problem I was having - thanks!

Nis

Worked without problems - i used the script to write cell arrays of mixed numbers and text.

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!