【问题标题】:Matlab Fieldnames indexing [duplicate]Matlab字段名索引[重复]
【发布时间】:2014-10-19 21:02:46
【问题描述】:

所以我有一个包含多个表的元胞数组,我正在尝试访问表的第一列名称。

c={'table1', 'table2', 'table3'}

以下两行都给了我错误:

fieldnames(c{1})(1) 
fieldnames(c{1}){1}

Error: ()-indexing must appear last in an index expression.

但如果我遵循,它会起作用:

fn=fieldnames(c{1})
fn{1}

有没有办法用一行代码做到这一点,有人可以解释一下错误吗?

【问题讨论】:

  • 谢谢 Banana,我正要这么做。

标签: matlab indexing field-names


【解决方案1】:

我假设以下代码重现了您的数据结构:

col1 = [1; 2; 3];
col2 = [4; 5; 6];
t1 = table(col1, col2);
t2 = table(col1, col2);
t3 = table(col1, col2);
c = {t1, t2, t3};

如果是这样,那么这应该有效:

subsref(fieldnames(c{1}),substruct('{}',{1}))

【讨论】:

    【解决方案2】:

    See this question.

    一般而言,此类问题可以使用函数调用(仅返回输入的虚拟函数)来解决,或者您可以将 fn{} 替换为显式调用 subsref

    subsref(fieldnames(c{1}),substruct('{}',{1}));
    

    关于您关于错误发生原因的问题 - maybe this link could help

    【讨论】:

    • 我收到以下错误:SUBSREF 和 SUBSASGN 的下标参数的“subs”字段必须是单元格或字符数组。
    • 我在substruct 内的1 周围缺少{}。现在应该可以工作了。您应该查看我提到的链接以获取更多信息。
    • @Dev-iL 链接到的问题看起来正是您所需要的。匿名函数看起来是一个很好的解决方案。 subindex = @(A,r,c) A(r,c); s = subindex(fieldnames(c{1}), 1, 1);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    相关资源
    最近更新 更多