File Exchange

image thumbnail

struct2csv

version 1.4 (1.87 KB) by

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

4.90909
11 Ratings

141 Downloads

Updated

View License

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

Comments and Ratings (22)

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)
fprintf(FID,l);

help please!

Sara Bahloul

Very useful! Ty

Lee Chin

Manuel Tei

tzuting chen

needs a csv2struct!!! really!!

Serge

Serge (view profile)

needs a csv2struct

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!

Great time saver, exactly what I needed. Thanks.

Jaap de Vries

Jaap de Vries (view profile)

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.

Paul

Paul (view profile)

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

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

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.

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.

David

David (view profile)

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

Ferad

Ferad (view profile)

Kate

Kate (view profile)

Nevermind the previous comment, simple path issue.

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

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

1.4

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

MATLAB Release
MATLAB 7 (R14)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today