File Exchange

image thumbnail

struc2xls

version 1.0 (2.34 KB) by

Writes a data structure in MATLAB to an excel file.

10 Downloads

Updated

View License

Modified from Francisco de Castro's code, struc2xls lets you save data structures containing a mix of numeric and non-numeric data to an Excel file. This version runs faster than the original and has several extra output options.

STRUC2XLS (FILE,S) writes the data structure S to an Excel workbook named FILE in the current directory. Data fields can be numeric, non-numeric, or mixed. Output can be oriented vertically (field names in first column, data in rows) or horizontally (field names in first row, data in columns). I'm sure there's room for improvement here. Let me know...

EXAMPLES:
Create a sample structure 's' with two fields:
s=struct('Name',{'Jon','Jonathan','Johnny'},'Score',[85,100,75]);

Basic output to file 'demo.xls' in current directory:
struc2xls('demo',s)

Output beginning in cell D4 of Excel file:
struc2xls('demo',s,'Col','D','Row',4)

Output to specified worksheet 'Students':
struc2xls('demo',s,'Sheet','Students')

Output horizontal data (header row, data in columns):
struc2xls('demo',s,'Orientation','H')

Output to alternate directory
struc2xls('C:\Users\Johnny\Desktop\demo',s)

Comments and Ratings (7)

Shubo Wu

Undefined function 'struct2cell' for input arguments of type 'char'.
Error in struc2xls (line 83)

Hi, Haven't got it to work, it seems to not like empty entries in structures (e.g. data.x=[])

------------------------------------------
??? Error using ==> xlswrite at 154
Input array is empty.

Error in ==> struct2xls at 55
xlswrite(filename,m,sheet,rangeB);
------------------------------------------

working with your example :)

nice...

but not working with my file...

Input data must be a numeric, cell, or logical array.

Error in ==> struct2xls at 55
xlswrite(filename,m,sheet,rangeB);

Joana

Joana (view profile)

Hi, in my case I had the same

??? Error using ==> ctranspose
Transpose on ND array is not defined.
Error in ==> struc2xls at 91
out=[names';c'];

And I solved it by squeezing c in line 83 as:

%Transform to cell
c= squeeze(struct2cell(s));

Hope it helps!

seems to work fine with me....

David

David (view profile)

Strange : Program does not seem to work with your example

>> s=struct('Name',{'Jon','Jonathan','Johnny'},'Score',[85,100,75]);
>> struc2xls('demo',s)
??? Error using ==> ctranspose
Transpose on ND array is not defined.

Error in ==> struc2xls at 91
out=[names';c'];

Ofek Shilon

Nice effort - but i'd given it 5 if it only handled nested structs.

MATLAB Release
MATLAB 7.6 (R2008a)
Acknowledgements

Inspired by: struc2xls

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