MATLAB: How to: For loops for Excel Tab

for loops

I need to read some data out of couple excel tabs (1 file). The only thing that changes is the tabname. The code works, but it becomes too long if you going to read 6 tabs, so i want it in a for loop then varie the tabname. What i want saved of each tab is: the num(2,13) (second line in code) and sum (last line). How can I do this easiest way?
[num, txt]=xlsread(filename,'*Tabname*');
*Tabname*=num(2,13);
long code
*sumTabname*=sum(numeachcell);

Best Answer

If you know the names of your sheets, you can create a cell array containing those names. The output from the xlsread function will contain only the numeric data from the spreadsheet, and it is read into a cell in the variable data. I also created a variable named sums that contains the sum of all the data.
filename = 'Book1.xlsx';
sheetnames = { 'SheetA','SheetB','SheetC' };
n = length(sheetnames);
data = cell(n,1);
sums = zeros(n,1);
for idx = 1:n
data{idx} = xlsread(filename,sheetnames{idx});
sums(idx) = sum(data{idx}(:));
end