Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading Excel file with multiple sheets

I have an Excel file which contains 4 sheets.

Each sheet has the same format but the data inside is different. For example:

sheet1:
              sub1 sub2 sub3

    person1    2    3     4
    person2    9    0     1
    person3    8    4     2

sheet2:

              sub1 sub2 sub3

    person1    5    7     8
    person2    1    3     7
    person3    4    1     3

Now, I know how to read the data for 1 sheet:

[data, titles] = xlsread(FileName, 'sheet1');

But when I don't know how many sheets I have in my document, how can I store all data from all sheets?

like image 586
Ben Avatar asked Dec 01 '25 00:12

Ben


1 Answers

You can use xlsfinfo to get a list of the sheets and then loop over that list:

[status,sheets] = xlsfinfo(FileName)

for s = 1:numel(sheets)
    ...
    [data,titles]=xlsread(FileName,sheets(s))
    ...
end
like image 188
Dan Avatar answered Dec 03 '25 17:12

Dan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!