MATLAB Answers

I have data in the same excel workbook but in 10 different work sheets and i'm trying to run a loop that will read in the data from each work sheet consectutively.

2 views (last 30 days)
James Oxborrow
James Oxborrow on 29 Aug 2018
Commented: James Oxborrow on 29 Aug 2018
this is how i've tried to do it so far and i'm not sure how to get it to work
for ii=01:10
data(ii)=xlsread('workbook.xlsx','worksheet(ii)','B10:F370');
end

  0 Comments

Sign in to comment.

Accepted Answer

KL
KL on 29 Aug 2018
You could use xlsread along with xlsinfo.
[status,sheets] = xlsfinfo(filename);
Now, you have all the sheet names under sheets and you loop through that.
data = cell(numel(sheet),1); %pre-allocate your variable
for sheetNo = 1:numel(sheet)
data(sheetNo,1) = xlsread(filename,sheets{sheetNo},Range);
...
end
Goodluck!

  3 Comments

James Oxborrow
James Oxborrow on 29 Aug 2018
ok, I understand what that code is doing but i'm stuggling to translate it into my data. What about this have i done wrong?
[status,sheets] = xlsfinfo('cyclist_data');
data = cell(10,1);
for sheetNo = 1:10
data(sheetNo) = xlsread('cyclist_data','sheets{sheetNo}','B10:F370');
end
KL
KL on 29 Aug 2018
  1. Your filename needs an extension --> cyclist_data.xlsx
  2. 'sheets{sheetNo}' is not how you use the variable sheets. Just look at how I used it in my example!

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Products


Release

R2018a