How to save struct data from struct to excel?

290 ビュー (過去 30 日間)
Ann G
Ann G 2015 年 12 月 4 日
コメント済み: Kip Risch-Andrews 2022 年 11 月 9 日
How can I save the output data of my matlab code in an excel file when the data are struct?

採用された回答

Guillaume
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 件のコメント
elz tomz
elz tomz 2018 年 7 月 4 日
Thank you Guillaume. It works fine
Muhammad Amirul Hafiz Sahful Bahri
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
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 件のコメント
Varun Vakayil
Varun Vakayil 2022 年 8 月 16 日
Thank you so much Raj. This was exactly what I was looking for.
Kip Risch-Andrews
Kip Risch-Andrews 2022 年 11 月 9 日
This works perfectly for structures with many substructs, thanks for posting it!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by