How to save struct data from struct to excel?
조회 수: 270 (최근 30일)
이전 댓글 표시
How can I save the output data of my matlab code in an excel file when the data are struct?
댓글 수: 0
채택된 답변
Guillaume
2015년 12월 4일
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
Muhammad Amirul Hafiz Sahful Bahri
2019년 5월 8일
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?
추가 답변 (1개)
Raj Sodhi
2019년 9월 29일
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
Kip Risch-Andrews
2022년 11월 9일
This works perfectly for structures with many substructs, thanks for posting it!
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!