Code covered by the BSD License  

Highlights from
struct2csv

4.83333

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

struct2csv

by

 

02 Feb 2012 (Updated )

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

| Watch this File

File Information
Description

STRUCT2CSV(s,fn)

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, james.slegers_at_gmail.com

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);
end
%%Finish added code
else
if isnumeric(c)
for kk = 1:sz(jj,2)

Thanks!

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  
03 Dec 2013 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).

28 Nov 2013 benny

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

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.

27 Aug 2013 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.

26 Aug 2013 David

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

21 Jul 2013 Ferad  
24 Jun 2013 Kate

Nevermind the previous comment, simple path issue.

24 Jun 2013 Kate

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

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

18 Apr 2013 Sebastian

Hi,

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),'",'];
else
str = ['"',c{ii,kk},'",'];
end

Updates
18 Jun 2013

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

Contact us