File Exchange

image thumbnail


version (1.87 KB) by James Slegers
Output a structure to a .csv file, with column headers


Updated 18 Jun 2013

View Version History

View License


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

Cite As

James Slegers (2021). struct2csv (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (35)


Maybe a correction need to tackle boolean variables. See program below and its error

struct2csv(base, 'MyFile.txt');
Error using horzcat
The following error occurred converting from logical to char:
Conversion to char from logical is not possible.

Error in struct2csv (line 91)
str = [str,'"',c{ii,kk},'",'];

Peng Wang

I tried to write some datetime variables in MATLAB to csv with this file. The names of the variables are written to the csv file, but the variables themselves, I mean, the date and time, are not written to the csv file. Does anyone know what is the problem?

kinkini bhadra

Works efficiently and easy to add. Thanks !

Gershon Wolfe

Brice Cannon

Easy to use and works as expected!

Simon Blackmore

Thanks James and Joost.

Joost Meekes

Wonderful, only problem I run into is that as Solange mentions, it repeats the column headers above each entry in the struct. This is easily fixed though, for example by putting an if-statement around the line that prints them (line 57), i.e.:
if rr == 1

Gibran Benitez

Lan Li Yu


Menglin Pang


How can I use this function without exporting the column headers? My csv files repeat the headers above each row of data.

Jonas Goltz

Gerd Rösser

I can confirm that it runs very well in octave, too!
Thank you!!!

Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.

Error in struct2csv (line 57)

help please!

Sara Bahloul

Very useful! Ty

Christoph Ressel

Lee Chin

Manuel Tei

tzuting chen

needs a csv2struct!!! really!!


needs a csv2struct

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)


Francois Hugon

Great time saver, exactly what I needed. Thanks.

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.


James Slegers

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


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


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.

James Slegers

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.


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



Nevermind the previous comment, simple path issue.


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

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



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},'",'];

MATLAB Release Compatibility
Created with R14
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!