View License

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

» Watch video

Highlights from
Cell Array to CSV-file [cell2csv.m]

4.7 | 35 ratings Rate this file 149 Downloads (last 30 days) File Size: 1.89 KB File ID: #4400 Version: 1.1

Cell Array to CSV-file [cell2csv.m]


Sylvain Fiedler (view profile)

  • 1 file
  • 4.74286


22 Jan 2004 (Updated )

Writes a cell array into a comma separated values text file (*.csv)

| Watch this File

File Information

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.


This file inspired High Throughput, Algorithmic Determination Of Nanoparticle Structure From Electron Microscopy Images.

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (41)
21 Feb 2017 Flo Hem

20 Feb 2017 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:

13 Feb 2017 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

Comment only
29 Nov 2016 Rafael Rodriguez

22 Oct 2016 Zac Giles

17 May 2016 Jan Berling

28 Apr 2016 rd2047

rd2047 (view profile)

15 Oct 2014 Ricardo

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 ;)

Comment only
02 Oct 2014 Charlotte

02 Oct 2014 Fabricio Pereira

26 Sep 2014 Alex

Alex (view profile)

24 Jun 2014 Jerry

Jerry (view profile)

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.

24 Jun 2013 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%.

17 Jun 2013 Rostam FarrokhNejad

24 Apr 2013 Jan Simon

Jan Simon (view profile)

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.

05 Mar 2013 Igor

Igor (view profile)

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

31 Jul 2012 geieraffe

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

Comment only
29 Mar 2012 Pearl

Pearl (view profile)

06 Mar 2012 Reza Farrahi Moghaddam

07 Sep 2011 Wouter

Wouter (view profile)

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

Comment only
09 Aug 2011 Robert

Robert (view profile)

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

Comment only
13 Jan 2011 Irene

Irene (view profile)

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

17 Mar 2010 Ivan Brezani

02 Sep 2009 Anastasia

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.

26 Sep 2006 helen chen

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

02 Aug 2006 R B

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

Comment only
02 Aug 2006 R B

Buggy. Breaks fprintf in some cases

23 May 2006 Wouter Rogiest

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

21 May 2006 Joe Lotz

Very help, thanks alot for sharing!

08 Mar 2006 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

26 Oct 2005 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!

16 Sep 2005 Mike B

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

15 Sep 2005 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)

04 Aug 2005 Miklos Argyelan

10 Jun 2005 P L

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

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

05 May 2005 R K

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

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

27 Apr 2005 C T

23 Mar 2005 Marcelo Zeri

Excelent, exactly what I need.

17 Mar 2005 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

10 Aug 2004 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:

13 May 2004 Ted Skolnick

Simple and effective.

24 Jan 2005

Updated to work easier with OfficeXP.

31 Jan 2005

adapted separator for OfficeXP

09 Mar 2006

Improved file to wirk with Excel 97,2000 oder alternatively with Excel 2003.
Microsoft changed the default seperator, so now when Excel Version is > 2003, it is taken care of.

03 Nov 2010 1.1

Added the choice of the decimal separator

Contact us