Matlab字段名称索引
问题描述:
所以我有一个包含多个表的单元格数组,我正在尝试访问该表的第一列名称.
So I have a cell array with multiple tables and I am trying to access the first column name of the table.
c={'table1', 'table2', 'table3'}
以下两行都给我错误:
fieldnames(c{1})(1)
fieldnames(c{1}){1}
Error: ()-indexing must appear last in an index expression.
但是,如果我执行以下操作,它将起作用:
But if I do following, it works:
fn=fieldnames(c{1})
fn{1}
有一种方法可以用一行代码来做到这一点,有人可以解释该错误吗?
Is there a way to do this with one line of code and can someone please explain the error?
答
通常,可以使用函数调用(仅返回输入的虚拟函数)解决此类问题,也可以仅用显式调用
Generally such problems can be solved using a function call (either a dummy function that just returns the input) or you could just replace the fn{} with an explicit call to subsref
:
subsref(fieldnames(c{1}),substruct('{}',{1}));
Regarding your question about why the error happens - maybe this link could help.