How to save struct data from struct to excel?

How can I save the output data of my matlab code in an excel file when the data are struct?

 Accepted Answer

We could do with a lot more details about what you want to do. Do you want the field names to be saved? Is the structure scalar or an array? But at the simplest:
writetable(struct2table(yourstructure), 'someexcelfile.xlsx')
Note that Excel is a tabular format, whereas structures are not. The two are not really the same thing.

11 Comments

Thank you but unfortunately it doesn't work..I get this message "??? Undefined function or method 'struct2table' for input arguments of type 'struct'."
You must be using an old version of matlab. struct2table was introduced in 2013b if I recall correctly.
You can use
xlswrite('someexcelfile.xlsx', struct2cell(yourstructure))
but if you want the field names as header, you need to add them yourselves at the first row of the cell array.
Yes, actually I use 2007b! Thank you very much!
This command works to a point.. An excel file has been created and it has regognize 20 values that I need but it is still empty! Actually the structure and the values are from a function in my main code..Do you think this has something to do with my problem?
As I said to start with, more details about your structure and how you want to save it are required.
My guess is that the missing data is because the field of the structure is not a scalar number or string. Maybe another structure, maybe an array? If that is the case, how are you expecting that to appear in excel?
The best thing would be to show us an example of the structure and an example of the expected excel file that goes with it.
I use the function GLCM_Features: [out]=GLCM_Features(glcm,0) and I want all the values of the calculated features to be saved in an excel file so that I will be able to use them for statistical calculations. The Workspace shows me that out is an 1x1 struct.
I'm assuming you meant this function that is called GLCM_Features1 and not GLCM_Features. Don't make it harder to answer your question, in the future provide links.
Assuming this is the correct function, then out is indeed a 1x1 structure, and each field is a 1xk row vector, where k is the third dimension of your glcm. In that case:
c = struct2cell(out);
xlswrite('somefile.xlsx', vertcat(c{:});
%or if you want the field names:
xlswrite('somefile.xlsx', [fieldnames(out), num2cell(vertcat(c{:}))]);
Thank you very much! And I'm sorry for causing you any trouble!I new in all this!
You're not causing trouble. I was trying to convey to you that giving more (and accurate) details of what you have and want would make it much easier to answer your question.
In this case, we needed to know that you had a scalar structure where all the fields contained row vectors of identical size.
The solution was therefore to concatenate the row vectors vertically into a matrix and pass that to excel.
Thank you Guillaume. It works fine
I have an error something like this,
Error using struct2table (line
26)
Input structure must be a
scalar structure, or a
structure array with one column
or one row.
Error in beginner (line 17)
writetable(struct2table(S{1,1}),'someexcelfile.xlsx');
How can i solve this problem?

Sign in to comment.

More Answers (1)

In some cases you have a struct as an element of a struct. So I found it necessary to find only those an output those to the Excel file.
txt = fieldnames(strct) ;
sel = ones(size(txt)) ;
for i = 1:length(txt)
sel(i) = isstruct(strct.(txt{i})) ;
end
i_not_struct = find(~sel) ;
i_struct = find(sel) ;
x = [fieldnames(strct) struct2cell(strct)] ;
xlswrite(xlsfile ,x(i_not_struct,:),1,'a1') ; % winopen(xlsfile)
Then I treat the struct elements separately, as shown in the attached file.
Yours,
Raj

2 Comments

Thank you so much Raj. This was exactly what I was looking for.
This works perfectly for structures with many substructs, thanks for posting it!

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!