How to: For loops for Excel Tab

4 ビュー (過去 30 日間)
Hello kity
Hello kity 2012 年 12 月 24 日
コメント済み: Aljanadi 2017 年 12 月 7 日
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);

採用された回答

Laura Proctor
Laura Proctor 2012 年 12 月 24 日
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
  1 件のコメント
Aljanadi
Aljanadi 2017 年 12 月 7 日
I just want to say thank you
Looooooove you

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

その他の回答 (3 件)

Laura Proctor
Laura Proctor 2012 年 12 月 24 日
I'm not sure exactly what you are asking with regard to num(2,13). But here is some code that will read in different tabs of an Excel file in a loop and can hopefully get you started.
filename = 'Book1.xlsx';
data = cell(3,1);
for idx = 1:3
sheetname = ['Sheet',num2str(idx)];
data{idx} = xlsread(filename,sheetname);
end

Hello kity
Hello kity 2012 年 12 月 24 日
I want 2 data as output from every sheet. the value in that num and the sum.
your code is for sheets1,2,3,4, my sheets are not numbered, just random names.
how can i define the sheetnames and let them use in the forloop?

Hello kity
Hello kity 2012 年 12 月 24 日
This worked for me. Really useful information.
thank you
I have another question, see my profile,
it is about putting an enter in the x-axes (xtick), for example i have this below the first bar (in a bar graph)
Bar1 5/110 , but i want bar1 enter ('second line') 5/110

カテゴリ

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