Code covered by the BSD License  

Highlights from

4.8 | 6 ratings Rate this file 134 Downloads (last 30 days) File Size: 1.87 KB File ID: #34889 Version: 1.4
image thumbnail



James Slegers (view profile)


02 Feb 2012 (Updated )

Output a structure to a .csv file, with column headers

| Watch this File

File Information


Output a structure of matrices and cell arrays to a comma delimited file with field names as column headers. A simple method for exporting data structures.

      s : any structure composed of one or more matrices and/or cell arrays
     fn : file name

Written by James Slegers,

Covered by the BSD License

Required Products MATLAB
MATLAB release MATLAB 7 (R14)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (13)
20 Jun 2014 Rodrigo Cavalcanti

Excellent file. Just had wrong imports when the fields were blank ([]). Fixed it by changing the following:
for jj = 1:m
c = s(rr).(headers{jj});
str = '';
if sz(jj,1)<ii
str = repmat(',',1,sz(jj,2));
%% Added code
if sz(jj,2) ==0 %MOdified
str = repmat(',',1,1);
%%Finish added code
if isnumeric(c)
for kk = 1:sz(jj,2)


06 Mar 2014 Francois Hugon

Great time saver, exactly what I needed. Thanks.

20 Feb 2014 Jaap de Vries

Very Nice Job!!!!

You saved me a ton of time today.
One nice addition would be a possibility to put a second row with units below row one that show the fields. I notice that many of my engineering data files have two header lines; one width the field names and the second line with the units.

16 Dec 2013 Paul

Paul (view profile)

03 Dec 2013 James Slegers

James Slegers (view profile)

Nerual, please give an example of a matrix or array that gives this error. The function takes a number of different input types (cell arrays, matrices, etc), so it is not immediately clear to me what would cause that error.

Benny, again, please give an example of the matrix or array that causes this error. As to nested structs, those are not handled by this function, because it is not recursive, and it's not clear to me how such information could be laid out nicely in a tabular worksheet (.csv).

Comment only
28 Nov 2013 benny

benny (view profile)

I would give 5 stars if only I could get the [] read correctly, currently your program skips them and fill in the [] with the next cell-value, also if there are nested struct, it will not read it either, which will make the columns go wrong in Excel, I am a new MatLab user - is there a way to fix this problem ? Please help ASAP. Thanks, in advance

28 Aug 2013 Nerual

Nerual (view profile)

somehow the csv file contains some numbers without decimal point and therefore creates new numbers.In the struct there are no errors. How can that be? The struct only contains doubles.

Comment only
27 Aug 2013 James Slegers

James Slegers (view profile)

The precision is controlled by one of the numerous "num2str" arguments in the file. I did not give any specific number format strings, because it was made to be general-purpose. I'll put some thought to it, see if I can improve it. In the meantime, if you need the precision, look for the right "num2str", and pass it a format string.

Comment only
26 Aug 2013 David

David (view profile)

The resulting csv file has lower precision than the input data (type double). Anyway to change this?

Comment only
21 Jul 2013 Ferad

Ferad (view profile)

24 Jun 2013 Kate

Kate (view profile)

Nevermind the previous comment, simple path issue.

Comment only
24 Jun 2013 Kate

Kate (view profile)

Any clue why I would get this type of error message?

>> struct2csv(sitetable, 'info.csv')
Undefined function 'struct2csv' for input arguments of type 'struct'.

Comment only
18 Apr 2013 Sebastian


thanks a lot for this file!

But I found a little bug when a column is a "n x 1 char".

I fixed it with "an elseif":
c = getfield(s,headers{jj});
d = size(c);
if isnumeric(c)
str = [num2str(c(ii,kk)),','];
elseif d(2)==1
str = ['"',c(ii),'",'];
str = ['"',c{ii,kk},'",'];

Comment only
18 Jun 2013 1.4

Rewrote significant portions of code, in response to noted bugs. Hopefully, the noted bugs have been remedied.

Contact us