excel - 从 Excel 选项卡在 Matlab 中自动创建和命名表

标签 excel matlab loops matlab-table

我有以下单元格数组,它是 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/

相关文章:

vba - 检查项目是否在 ParamArray VBA 中

vb.net - 在 VB.NET 中调用 Marshal.FinalRelease 后 Excel 的实例仍然存在

c++ - 如何将 CSV 文件导入 QTableWidget

c# - 使用 Excel Interop w/C# 将 Excel 单元格内容居中

excel - `Application.StatusBar` 是否在 Office 2011 中工作?

matlab - 为什么 "pi"在 MATLAB 中成为符号?

jQuery Loop 动态变量生成

javascript - For 循环没有按预期工作,我做错了什么?

matlab - Matlab 中包含未知常数的矩阵的简化行阶梯

matlab - 对角矩阵求和 - MatLab