Saving struct to excel

131 ビュー (過去 30 日間)
Deepa Maheshvare
Deepa Maheshvare 2020 年 5 月 19 日
コメント済み: Geoff Hayes 2020 年 5 月 20 日
I've a struct in the following form
Astruct =
struct with fields:
data1: [5001×16 double]
data2: [5001×16 double]
I'd like to save this to an excel with the data corresponding to each fieldname in separate sheets of the same excel file. The sheets names have to be slightly modified
i.e derived from fieldnames : data1_matlab, data2_matlab.
Any suggestions on how to do this will be really helpful

採用された回答

Geoff Hayes
Geoff Hayes 2020 年 5 月 19 日
Deepa - you can use fieldnames to return a cell array of the field names for your structure. You can then iterate over each name in this array to extract the data and write it to an Excel worksheet. Perhaps something like the following will work
excelFilename = 'someFile.xlsx';
structFieldnames = fieldnames(myStruct); % <--- where myStruct is your struct of data
for k = 1:length(structFieldnames)
fieldname = structFieldnames{k};
writematrix(myStruct.(fieldname), excelFilename, 'Sheet', sprintf('%s_matlab', fieldname));
end
Please note that I haven't tested the above. See writematrix for more details.
  2 件のコメント
Deepa Maheshvare
Deepa Maheshvare 2020 年 5 月 20 日
編集済み: Deepa Maheshvare 2020 年 5 月 20 日
Is it possible to add column headers before writing to excel sheets?
For example ,
data1: [5001×16 double] % column header like this = ['col1', ..., 'col16']
I want to do this.
Thanks,
Deepa
Geoff Hayes
Geoff Hayes 2020 年 5 月 20 日
From writematrix name-value pair arguments, take a look at the Range property...you should be able to use that to write your header and then write your data.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by