我有以下单元格数组,它是 excel 文件中一些(但不是全部)选项卡名称的列表:
chosenTabs =
'Screen' 'SectorAbsolute' 'SectorRelative'
如何让它根据此列表中的内容读取 Excel 工作表的每个选项卡并返回选项卡内容的表格?新表应与读取的选项卡同名。
我已经尝试过(例如创建一个名为“SectorAbsolute”的表 包含选项卡“SectorAbsolute”的内容):
char(chosenTabs(2))=readtable(inputFile,'Sheet',char(chosenTabs(2)))
但这会返回错误:
You can not subscript a table using only one subscript. Table subscripting requires both row and variable subscripts.
最佳答案
利用 structure array 的方法:
chosentabs = {'Sheet1', 'Sheet3'};
ntabs = length(chosentabs);
for ii = 1:ntabs
mytables.(chosentabs{ii}) = readtable('test.xlsx', 'Sheet', chosentabs{ii});
end
返回 mytables
,一个包含您的表的结构数组。您可以显式访问您的工作表(例如 mytables.Sheet1
)或使用 dynamic field referencing (例如 mytables.(somestring)
)
关于excel - 从 Excel 选项卡在 Matlab 中自动创建和命名表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34185068/