File Exchange

image thumbnail

ANY2CSV

version 1.0.0.0 (4.38 KB) by Felix Zoergiebel
Exports any type of MATLAB data to a nicely formated csv-file

9 Downloads

Updated 27 May 2008

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

Exports any type of MATLAB data to a nicely formated csv-file, including fieldnames of structs and any multidimensional subscript-indices.
This tool is especially useful if you want to save the content of large structs and cell-arrays to an excel or ods sheet. Any to csv works recursively until all the content of a complicated object is written to a csv-spreadsheet and opens it in the standard csv application. There you can save it to e.g. xls or ods.

Cite As

Felix Zoergiebel (2020). ANY2CSV (https://www.mathworks.com/matlabcentral/fileexchange/19149-any2csv), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (16)

Craig Cole

Not overly pretty, but works when 'who' lists a variable, but workspace doesn't show it to navigate manually. (Tried openvar('varname'), which doesn't work either.) Bonus is you don't need to drill down into the structure like you would otherwise. Just have the function output to the screen as is shown in the example in the file.

Carter

Hey,

I am currently using any2csv (<http://www.mathworks.com/matlabcentral/fileexchange/19149-any2csv>) to output my mat file as a csv.

I am getting an output with the headers as seen below.

>> Structure1
>>Field,>> Field2,>> Field3,>> Field4,>> Field5, >> NestedStruct1
Data,Data,Data,Data,Data >>Field1,>>Field2,>>Field3,>> Field4,>> Field5
Data,Data,Data,Data,Data
I would like to output this data as one row of headers with the data pertaining to that header below it while containing the information about the structure format in the header. This can be seen in the other attached image.

Structure1_Field1,Structure1_Field2,Structure1_Field3,Structure1_Field4,Structure1_Field5,Structure1_NestedStruct1_Field1,Structure1_NestedStruct1_Field2,Structure1_NestedStruct1_Field3,Structure1_NestedStruct1_Field4,Structure1_NestedStruct1_Field5
Data,Data,Data,Data,Data,Data,Data,Data,Data,Data

I have changed the layout to this from the original version (lines 122-127)

%left right top bottom
gap_c=struct('l',0,'r',0,'t',0,'b',1); % gaps around cell elements
gap_s=struct('l',0,'r',0,'t',0,'b',0); % gaps around struct-fields
gap_n=struct('l',0,'r',0,'t',0,'b',0); % gaps around arrays
gap_t=struct('l',0,'r',0,'t',0,'b',0); % gaps around text
gap_content=struct('l',0,'t',1); % gap between titles and content. Both must be >=0, sum should be >0

As I rarely need to work with recursive functions I am having difficulty making this last change. Would you know how to change it or what I should change in the code?

Thanks in advance!

This does not work with a structure that has nested field names.

MATLAB 2012a, <1305983x3 cell>, sucked all my RAM, I ended up using http://www.mathworks.com/matlabcentral/fileexchange/7601-cell2csv instead

Hugh

very useful

Thanks, very useful

Larissa

Hi Felix,
Could you please have a look at this thread?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/281952#744609
I am out of memory in 2009b. What could be a reason for that?

sophie neve

very useful !!
but I find the opening of excel annoying
I also got a (little) problem when creating the file, it says :
The system cannot find the file ''pathname''
normal it does not found... has not been created yet....

so.

Joachim

Very usefull work ( see comment from Steffen).

Steffen

This is a great way to manage the workflow of your data from Matlab to any office-programm like excel or openoffice. It's handy and easy to use.

I am using OpenOffice.org instead of Microsoft Office. OpenOffice.org has a very nice import function for csv files. Once the file is imported you can save as XLS. I know that those functions are available via command-line tools, however I do not remember the names and I have never used them.

Puifai S

Do you know of a way to convert from CSV to XLS quickly?

Felix Zörgiebel

Shame on me, I should have tested that case... Thanks Urs, I corrected the bug. Felix

Urs (us) Schwarz

does NOT work with empty cell entries, eg,
c={'a',[],{}}
us

Updates

1.0.0.0

New feature: struct fields can be arranged vertically or horizontally. Setting in line 128
New feature: horz. and vert. distance between subscript/fieldname and content of cell/structfield can be set in line 127
Thanks Jiro Doke for your comments!

- display struct fields horizontally or vertically -> line 128
- display content of cell/struct fields under or right to the subscripts/fieldname -> line 127

Thanks Jiro Doke for those two ideas!

- removed empty struct bug

The empty cell bug is now finally gone.

MATLAB Release Compatibility
Created with R2007a
Compatible with any release
Platform Compatibility
Windows macOS Linux