使用多张纸读取 Excel 个文件
Reading Excel file with multiple sheets
我有一个 Excel 文件,其中包含 4 sheets。
每个sheet格式相同,但里面的数据不同。例如:
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
现在,我知道如何读取 1 的数据 sheet:
[data, titles] = xlsread(FileName, 'sheet1');
但是当我不知道文档中有多少 sheet 时,如何存储所有 sheet 的所有数据?
您可以使用 xlsfinfo
获取工作表列表,然后遍历该列表:
[status,sheets] = xlsfinfo(FileName)
for s = 1:numel(sheets)
...
[data,titles]=xlsread(FileName,sheets(s))
...
end
for
循环是不必要的。 importdata
函数(在 R2006a 之前引入)可以轻松应对这种情况。例如,我有一个 excel 文件,其中包含 5 张数据,如下所示:
如您所见,它包含文本、数字和空格。在 R2017b 上,调用
xlsData = importdata('name-of-file.xlsx');
导致 struct
包含两个字段:data
(对于数值,保存 double
数组)和 textdata
(对于文本,保存 cell
数组),每个数组都有对应于 Excel 文件中的工作表名称的字段。
我有一个 Excel 文件,其中包含 4 sheets。
每个sheet格式相同,但里面的数据不同。例如:
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
现在,我知道如何读取 1 的数据 sheet:
[data, titles] = xlsread(FileName, 'sheet1');
但是当我不知道文档中有多少 sheet 时,如何存储所有 sheet 的所有数据?
您可以使用 xlsfinfo
获取工作表列表,然后遍历该列表:
[status,sheets] = xlsfinfo(FileName)
for s = 1:numel(sheets)
...
[data,titles]=xlsread(FileName,sheets(s))
...
end
for
循环是不必要的。 importdata
函数(在 R2006a 之前引入)可以轻松应对这种情况。例如,我有一个 excel 文件,其中包含 5 张数据,如下所示:
如您所见,它包含文本、数字和空格。在 R2017b 上,调用
xlsData = importdata('name-of-file.xlsx');
导致 struct
包含两个字段:data
(对于数值,保存 double
数组)和 textdata
(对于文本,保存 cell
数组),每个数组都有对应于 Excel 文件中的工作表名称的字段。