Write cell array into an excel file with multiple sheets

52 ビュー (過去 30 日間)
alexandra ligeti
alexandra ligeti 2023 年 9 月 7 日
編集済み: Florian Bidaud 2023 年 9 月 7 日
Hi all,
I have looked at previous examples but struggling to get this working. I have a cell array called gc and it is 1x10 long. I would like to store each structure within the cell array into a single excel spreadsheet with multiple sheets. For example I would like all the contents of gc{1x1} stored in a excel spreadsheet on sheet one with with the various titles, but gc{1, 2} should be on sheet two of the same excel workbook. I have attached two images to hopefully highlight what I am trying to do. I would like the resulting excel spreadsheet to be a single workbook with 10 sheets, with each sheet containing the named fields in the second image. How would I go about doing this?
Thank you so much in advance.

採用された回答

Florian Bidaud
Florian Bidaud 2023 年 9 月 7 日
編集済み: Florian Bidaud 2023 年 9 月 7 日
Untested but something like this should work. This is considering you have the same field names for all the structures, and that you have less than 26 fields per structure.
fn = fieldnames(gc{1,1});
for i = 1:length(gc)
writecell(fn','excel_file.xls','Sheet',i)
for j = 1:length(fn)
[~,sizey] = size(gc{1,i}.(fn{j}));
if sizey > 1
gc{1,i}.(fn{j}) = gc{1,i}.(fn{j})';
end
writecell(gc{1,i}.(fn{j}),'excel_file.xls','Range',[char(j-1+'A') '2'],'Sheet',i)
end
end
  2 件のコメント
alexandra ligeti
alexandra ligeti 2023 年 9 月 7 日
That is great, thanks so much. It works if I use writematrix instead of writecell. A quick questions, if I wanted the columns titled to the same names as the field where would one go about putting that? Would i modify the writecell (which is now wrietmatrix)?
Thanks again!!!!
Florian Bidaud
Florian Bidaud 2023 年 9 月 7 日
編集済み: Florian Bidaud 2023 年 9 月 7 日
It should be already done in this code with writecell(fn','excel_file.xls','Sheet',i) on the third line.
For this one you can't replace it with writematrix as these are text fields.

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

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by