Import/convert an Excel file with more worksheets into a structure with more fields, one field for each worksheet

12 ビュー (過去 30 日間)
Hi All,
I would like to import/convert an excel file which has n worksheets, into a structure with n fields, like S.f1, S.f2, ..., S.fn, one field for each worksheet of the Excel file, in MATLAB.
Is there anyone who could help me?
Thanks,
Mike
  1 件のコメント
Stephen23
Stephen23 2019 年 7 月 26 日
"into a structure with n fields, like S.f1, S.f2, ..., S.fn"
This would be better a non-scalar structure, with one field (or indeed, simply a cell array).

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

採用された回答

Bob Thompson
Bob Thompson 2019 年 7 月 26 日
Generally, importing multiple excel sheets is done by calling the xlsread command multiple times. One of the flags of the command is for designating which sheet you are using. The command does not import the information into a structure array, but you can reorganize the information from the imported matrix into a structure array easily enough.
for i = 1:nsheets
[~ ~ data] = xlsread('myexcelfile.xlsx',i);
[mystruct.data1] = data{range1};
[mystruct.data2] = data{range2};
[mystruct.data3] = data{range3};
[mystruct.data4] = data{range4};
end
Obviously you need to make your own ranges and structure fields, but the basic concept is sound.
  5 件のコメント
Guillaume
Guillaume 2019 年 7 月 26 日
編集済み: Guillaume 2019 年 7 月 26 日
Well, actually, I made a mistake and indeed should have made data a cell array
for i = 1:numel(sheets)
[~, ~, mystruct.data{i}] = xlsread(excelfile, i); %correct syntax!
end
edit: or another option is to make data a matrix and make the structure an array by indexing the structure rather than the field:
for i = 1:numel(sheets)
[~, ~, mystruct(i).data] = xlsread(excelfile, i);
end
Mike
Mike 2019 年 7 月 26 日
Thank you very much Guillame! I really appreciate your help.
Best regards,
Mike

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import and Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by