File Exchange

image thumbnail

cell2csv

version 1.0 (947 Bytes) by

Writes cell array content into a *.csv file.

4.29412
17 Ratings

29 Downloads

Updated

No License

This function writes a cell array into a CSV-file.
It works with empty cells, numeric cells, char cells, and string cells. One array can contain all of them.

CSV-files can then be read by Excel, so you don't need DDE or ActiveX.

Written by Sylvain Fiedler. I just fixed the delimiter mistake.

Comments and Ratings (21)

air101010

Such a nice idea that is totally ruined by the use of |eval|. Why do beginners think that |eval| is a solution to anything?

For a much better alternative that does not use |eval|, see these FEX submissions:

http://www.mathworks.com/matlabcentral/fileexchange/25387-write-cell-array-to-text-file

or

http://www.mathworks.com/matlabcentral/fileexchange/47055-cell-array-to-csv-file--improved-cell2csv-m-

For an explanation of why using |eval| is a bad way to code (no matter how much beginners love it):

http://www.mathworks.com/matlabcentral/answers/196952-how-to-create-new-variables-in-batches-with-strcat

Hopefully no beginners download this and learn to use |eval| for trivial code.

This is perfect. How I could add a row with headers?

Exceeding my expectations. Great job.

Pearl

Pearl (view profile)

kb

kb (view profile)

Thanks, it's useful. Numbers conversion is limited to 4 decimals tho. I added a decimals number option https://mega.co.nz/#!e5gHEZBL!VXEmHX8hqxvwK0ZvrtW1qldtu7dk2KuR7L_DAKQyt5Y

Jose Antonio

When the cell is logical doesn't work

João Neves

Thanks

works great (MATLAB 2012a, <1305983x3 cell>)

geieraffe

Is there an option to do this with "apped" like in DLMWRITE()?

This script would be perfect.

Murat Ates

xlswrite works much faster as Tim pointed. Only thing is I could not make it produce comma delimited cvs. I passed the 65536 row limit with Excel 2010. I found in some places they use xlFormat = 6 instead of 24 but both of them produces tab delimited csv.

“.csv”: FileFormatNum = 6
“.txt”: FileFormatNum = -4158
“.prn”: FileFormatNum = 36

Ivan Brezani

works fine, thanks!

Tim Sharp

For up to 65536 rows and 256 columns (or I assume more in Excel 2007 though I haven't tested), you can write to a csv file super-quick using a modified version of xlswrite. Simply add the enumeration xlFormat = 24 for when ext is csv in the switch-case around line 230 and you simply need to give xlswrite a filename that ends in csv.

Daniel Everson

This function is great. However, it seems that using "eval" on line 28 is unnecessary. By changing:
var = eval(['cellArray{z,s}']);
to:
var = cellArray{z,s};
you get a significant increase in preformance. 16.5 seconds -> .45 seconds in the example I tested for a large cell.

Xi Cheng

Wonderful, just what I need

Sergey Bondarchuk

Thanks, it's useful function.
However, there is a little bug if the cell contains logical values because
isnumeric( logical(1) ) returns 0.
The line 26 should be replaced by:
if isnumeric(var) || islogical(var)

Regards

Jalal A

Dude, I love you. I'm learning matlab trial by error. So far ok, al the difficult tasks of parsing I can do. Its the simple stuff like export to txt or csv from cell arrays that stump me :P Thanks for posting

Matt Hoffman

Great function. Only problem is that MATLAB's csvread requires .csv files to be entirely numeric--any chance of a csv2cell in the future, which can read in text data as well?

Delong Liu

Great job.

Paras Jethwani

This utility is quite helpful since I do not know of any way to export MATLAB data to a CSV file which is a very basic requirement.

Nevertheless, this file must also provide support for exporting simple arrays (minor changes required).

MATLAB Release
MATLAB 7.0.1 (R14SP1)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video