Working with structs & excel spreadsheets

2 ビュー (過去 30 日間)
Jo
Jo 2014 年 11 月 10 日
コメント済み: Jo 2014 年 11 月 10 日
Hi.
I have a big excel spreadsheet with multiple sheets (n=39) and I want to import it to matlab and do some calculations. I have used the command %importdata and I have all the data in a struct. I have already built a script in order to access the data of the first sheet (%t.data.S1(:,:)) and do some calculations. I need to do the same for the rest sheets (S2..S39) using a for loop. I tried this [type, sheetname] = xlsfinfo('.xlsx');
i = length(sheetname);
for sheet = 1:i
results (sheet)= t.data.S(sheet);
Z(sheet).results= results(sheet)
end
but doesn't work!
Any ideas?Many thanks in advance.

採用された回答

Orion
Orion 2014 年 11 月 10 日
編集済み: Orion 2014 年 11 月 10 日
You need to call xlsread at each iteration in order to read a new sheets.
XlsFile = 'YourXlsFile.xlsx';
% get the name of all your spreadsheets
[type, sheetname] = xlsfinfo(XlsFile);
% loop for each sheet
for i = 1:length(sheetname)
MyData{i} = xlsread(XlsFile,sheetname{i});
end
just adapat this piece of code to your problem.
Instead of
MyData{i} = xlsread(XlsFile,sheet{i});
just put something like
MyData{i} = YourImportdataScript(XlsFile,sheet{i});

その他の回答 (1 件)

Guillaume
Guillaume 2014 年 11 月 10 日
You need to dynamic field names to access your sheets in the structure. You can construct the field name (which must be a string) with sprintf:
results(sheet) = t.data.(sprintf('S%d', sheet));
  3 件のコメント
Guillaume
Guillaume 2014 年 11 月 10 日
Yes, sorry I just copied your code without thinking. Use a cell array for results:
results{sheet} = t.data.(sprintf('S%d', sheet));
Jo
Jo 2014 年 11 月 10 日
great!thank you very much for your help.

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

カテゴリ

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