File Exchange

image thumbnail

Cell Array to CSV-file [cell2csv.m]

version (1.89 KB) by Sylvain Fiedler
Writes a cell array into a comma separated values text file (*.csv)


Updated 03 Nov 2010

View Version History

View License

This function writes a cell array into a CSV-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.

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

Cite As

Sylvain Fiedler (2021). Cell Array to CSV-file [cell2csv.m] (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (56)


Alexandre Vandal

John Jones


Jacob Peplinski



I use this function regularly and it always worked great. However now I experience some issues. I want to save cells of different types to a txt file, which works well when I open the resulting text file in matlab. If however, I open the file outside matlab (e.g. with notepad) the rows of the cell are not separated as individual rows in the txt file.

An example:

I want this:
Header1, Header 2, Header 3
Data1, 999999, 888888
Data2, 77777,66666

but the resulting text file looks like this
Header1, Header 2, Header 3, Data1, 999999, 888888, Data2, 77777, 66666

Any ideas? Thanks a lot

minotshing maza

Geng Deng

This function doesn't work for cells with commas. For example, if I run

out = {'ReadMe, Inc.', 'ReadMe2, LLC'};
cell2csv( '\test.csv',out);

the resulting csv fille would have four cells, instead of two. Does anyone know how to resolve this problem?

François Hulot


Avishay Assayag

Hang Dong

so effective!

Kahfi Gunardi

Pearly Adinegoro


Flo Hem

Bogdan Gherman

Good and simple, but a bit slow.
Saved me the hassle of duplicating the code.

It can be enhanced by using 'W' instead of 'w' in fopen and is MUCH faster! Autoflush is very slow!

Check out this link:

Nathan Zimmerman

This was not working for me for 2d cell arrays of varying column sizes. To fix this, I changed the fprintf to the following:

fprintf(datei, '%s', var{1,:});


e.g. a 3d cell array

Rafael Rodriguez

Zac Giles

Jan Berling



Just FYI:

I had a problem with this script when my cell array had a row with strings: all the values were written as 'NA', supposedly because they didn't verify the conditions from all the elseif's of StringX(x).

If I deleted the last else it would give me an error: "Error using sprintf
Function is not defined for 'cell' inputs."

So I changed the last else from

x = 'NA';

x = char(x);

And it worked. No more 'NA'.

Thank you for your script. Good work ;)


Fabricio Pereira



I modified according to Justin and Jan's suggestions. And also added support for row vector. It is much faster now. See the updated file at

credits go to Sylvain.

Justin Su

A very convenient code. However, it is a little bit slow for saving large tables.
Jan Simon's suggestion is very helpful. Another way to further improve its speed is to bundle the output to fprintf() as much as possible. For example, write a whole line at one call rather than call fprintf() for each cell.
These two tweaks can reduce the excution time by >60%.



This function has a large potential for improvements:
"var = eval(['cellArray{z,s}']);" could be replaced by the much faster and nicer "v = cellArray{z,s};".
"if size(var, 1) == 0" does not catch [1x0] arrays, while "isempty(v)" would.

Checking the success of FOPEN is strongly recommended to avoid a strange behaviour.

The documentation is sufficient and the function works as advertised.


Nice function, but a tiny check for a proper result from fopen() might be nice (just for user to get better error messages).


is there a Version with "append", like Manish Mittal wrote on 15 Sep 2005?
Can't find it!
But would be great! I need exact this, ...just with an "append" option



csv2cell can be done by using a combination of uigetfile and textscan.


Nice m-script. Here a diff to account for cellstr contents and empty cells:
diff cell2csv.m cell2csv_orig.m
< if isempty(var)
< continue
< end
> % OUTPUT value
> fprintf(datei, '%s', var);
< if s ~= 1 %size(cellArray, 2)
> if s ~= size(cellArray, 2)
< % OUTPUT value
< if ischar(var)
< fprintf(datei, '%s', var);
< elseif iscellstr(var)
< fprintf(datei, '%s', var{1});
< end


Thanks; just used it to convert a 1048576x10 cell array to csv, took about 5 hours but didn't crash.

Ivan Brezani


Thanks for the great script, just a note, I modified the line 56 to allow for output with CR/LF termination as required by some windows and visual basic programs.


It may be worth adding as an option.

helen chen

so far it's working fine:) btw, you could use the textscan function in matlab 7 to convert csv to cell array


This is apparently an older version of another cell2cvs on and should be removed


Buggy. Breaks fprintf in some cases

Wouter Rogiest

I am using this in all my MATLAB code --thanx! but I can't help wondering if anyone has made a csv2cell ?

Joe Lotz

Very help, thanks alot for sharing!

Kevin Keegan

Is there a code for a reverse version of this -- csv2cell ? I've seen C versions but not Matlab ones -- having a bit of trouble writing one myself.
Thanks -- Kevin

Chris Curtis

This is a big help. I'm having a hard time wrapping my mind around cell arrays and this is perfect. Very flexible, simple, and user-friendly. Thanks!

Mike B

As per the comments above this is excellent. I'm using it just to generate text file output, so its perfect.

Manish Mittal

Here is another version of the same file with append, overwrite etc options. Also the delimeter issue is fixed

function cell2csv(filename,cellArray,delimiter,mode)
% Writes cell array content into a *.csv file.
% CELL2CSV(filename,cellArray,delimiter)
% filename = Name of the file to save. [ i.e. 'text.csv' ]
% cellarray = Name of the Cell Array where the data is in
% delimiter = seperating sign, normally:',' (it's default)
% mode = specifies the mode of opening the file. See fopen() for a detailed
% list (default is overwrite i.e. 'w')
% by Sylvain Fiedler, KA, 2004
% modified by Rob Kohr, Rutgers, 2005 - changed to english and fixed delimiter
if nargin<3
delimiter = ',';
if nargin<4
mode = 'w';

datei = fopen(filename,mode);
for z=1:size(cellArray,1)
for s=1:size(cellArray,2)

var = eval(['cellArray{z,s}']);

if size(var,1) == 0
var = '';

if isnumeric(var) == 1
var = num2str(var);


if s ~= size(cellArray,2)

Miklos Argyelan


In addition to R k's comment, change lines 32-34 to:

if s ~= size(cellArray,2)
fprintf(datei, trennZeichen);


since it is a csv it should be commas as the seperator.

if nargin<3,trennZeichen = ',';end


Marcelo Zeri

Excelent, exactly what I need.

Sergio Lucero

Great. Also needs to accept a different separator, so besides line 32 it needs to change line 12 to
if nargin<3,trennZeichen = ';';end

Juan F. Navas

It is simple, it is effective, and not only can write csv, it can read formats created by the user changing the "separator". Needs and adjustement at line 32:

Ted Skolnick

Simple and effective.

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!