Code covered by the BSD License  

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

4.7619

4.8 | 21 ratings Rate this file 208 Downloads (last 30 days) File Size: 1.89 KB File ID: #4400

Cell Array to CSV-file [cell2csv.m]

by Sylvain Fiedler

 

22 Jan 2004 (Updated 03 Nov 2010)

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  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (24)
13 May 2004 Ted Skolnick

Simple and effective.

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

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

23 Mar 2005 Marcelo Zeri

Excelent, exactly what I need.

27 Apr 2005 C T  
05 May 2005 R K

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

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

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

04 Aug 2005 Miklos Argyelan  
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);

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.

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!

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

21 May 2006 Joe Lotz

Very help, thanks alot for sharing!

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 ?

02 Aug 2006 R B

Buggy. Breaks fprintf in some cases

02 Aug 2006 R B

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

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 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.

17 Mar 2010 Ivan Brezani  
13 Jan 2011 Irene

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

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

07 Sep 2011 Wouter

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

06 Mar 2012 Reza Farrahi Moghaddam  
29 Mar 2012 Pearl  
Please login to add a comment or rating.
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.

27 Jun 2008

Fixed the bug when cell array contained a logical variable

03 Nov 2010

Added the choice of the decimal separator

Tag Activity for this File
Tag Applied By Date/Time
data import Sylvain Fiedler 22 Oct 2008 07:13:11
data export Sylvain Fiedler 22 Oct 2008 07:13:11
csv Sylvain Fiedler 22 Oct 2008 07:13:11
cell Sylvain Fiedler 22 Oct 2008 07:13:11
cell to file Sylvain Fiedler 22 Oct 2008 07:13:11
array to file Sylvain Fiedler 22 Oct 2008 07:13:11
write array Sylvain Fiedler 22 Oct 2008 07:13:11
cell to text Sylvain Fiedler 17 Nov 2008 08:39:17
array to text Sylvain Fiedler 17 Nov 2008 08:39:30
save csv Sylvain Fiedler 17 Nov 2008 08:41:02
save cell array Sylvain Fiedler 17 Nov 2008 08:41:46
save cell Sylvain Fiedler 17 Nov 2008 08:41:59
cell to ascii Sylvain Fiedler 17 Nov 2008 11:21:13
ascii Sylvain Fiedler 17 Nov 2008 11:21:19
export ascii Sylvain Fiedler 17 Nov 2008 11:21:27
cell to ascii Hyunjoo ΓΌ 19 Mar 2009 01:29:15
cell to text Souma Chaudhury 14 Jun 2009 19:14:32
array to text Ilya 05 May 2010 11:41:51
csv sr 02 Jun 2010 19:45:06
array to file LOI NGUYEN 07 Dec 2010 14:29:15
csv Dave Holden 12 Jan 2011 15:23:51
save csv Diana 09 Apr 2011 06:36:45
save csv Carl 12 Apr 2011 17:50:24
cell to file Ismail Delice 15 Apr 2011 10:41:02
array to file XUEFEI 04 Jul 2011 10:08:33
array to text XUEFEI 04 Jul 2011 10:08:51
cell XUEFEI 04 Jul 2011 10:08:57
cell to ascii XUEFEI 04 Jul 2011 10:08:58
cell to file XUEFEI 04 Jul 2011 10:09:00
cell to text XUEFEI 04 Jul 2011 10:09:02
csv XUEFEI 04 Jul 2011 10:09:04
data export XUEFEI 04 Jul 2011 10:09:06
data import XUEFEI 04 Jul 2011 10:09:08
ascii XUEFEI 04 Jul 2011 10:09:09
export ascii XUEFEI 04 Jul 2011 10:09:11
save cell XUEFEI 04 Jul 2011 10:09:12
save cell array XUEFEI 04 Jul 2011 10:09:13
save csv XUEFEI 04 Jul 2011 10:09:15
write array XUEFEI 04 Jul 2011 10:09:17
cell to text samuel osah 05 Aug 2011 08:37:26
export ascii Anat 24 Aug 2011 21:25:40
export ascii Huseyin 01 Sep 2011 06:36:52
array to file Huseyin 01 Sep 2011 06:37:01
array to file Csaba 06 Oct 2011 11:48:13
csv Mana 19 Oct 2011 00:59:53
array to file tom99 25 Oct 2011 00:20:17
array to file Yuming Cao 06 Dec 2011 11:16:00
cell to file huda nawaf 06 Dec 2011 17:08:47
array to file Monica 12 Dec 2011 20:11:05
array to text Srinivas 16 Dec 2011 10:10:39
array to file Madusudanan Sathia narayanan 06 Jan 2012 13:13:49
csv mehdi 15 Mar 2012 12:59:00
array to file Alex 11 Apr 2012 21:52:13
csv nilufar 23 May 2012 05:14:30
csv Ernest Kamavuako 25 May 2012 10:05:39

Contact us at files@mathworks.com