4.69231

4.7 | 26 ratings Rate this file 182 Downloads (last 30 days) File Size: 1.89 KB File ID: #4400

Cell Array to CSV-file [cell2csv.m]

by

 

22 Jan 2004 (Updated )

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

| Watch this File

File Information
Description

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.

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 (30)
24 Jun 2014 Jerry

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 http://www.mathworks.com/matlabcentral/fileexchange/47055-cell2csv-m

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 alireza KashaniPour  
24 Apr 2013 Jan Simon

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

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 Fabian

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

29 Mar 2012 Pearl  
06 Mar 2012 Reza Farrahi Moghaddam  
07 Sep 2011 Wouter

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

09 Aug 2011 Robert

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

13 Jan 2011 Irene

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.

fprintf(datei,'\r\n');

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 mathworks.com and should be removed

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 = ',';
end
if nargin<4
mode = 'w';
end

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 = '';
end

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

fprintf(datei,var);

if s ~= size(cellArray,2)
fprintf(datei,[delimiter]);
end
end
fprintf(datei,'\n');
end
fclose(datei);

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

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:
fprintf(datei,trennZeichen);

13 May 2004 Ted Skolnick

Simple and effective.

Updates
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

Added the choice of the decimal separator

Contact us