【问题标题】:Looking up data entries assigned to fields in structures查找分配给结构中字段的数据条目
【发布时间】:2018-06-16 19:34:41
【问题描述】:

我在 MATLAB 中有一个结构,每个字段有一百多个数据条目。

struct.p = [1 2 3 4 ...]

我希望能够通过输入数据条目来搜索字段。 例如,输入2 会告诉我该字段是'p'

我尝试将 isfieldfind 结合起来,但没有成功。

find(isfield(p(9)))

【问题讨论】:

    标签: matlab struct matlab-struct


    【解决方案1】:

    如果A是结构体数组,k是要在所有字段中搜索的值,那么:

    ind = structfun(@(x) any(x==k), A);  %Loop over all structure fields to search k 
    fn = fieldnames(A);                  %Name of all the fields
    kfield = fn(ind);                    %Name of those fields which have k
    

    struct 是一个内置函数,不要在你的结构中隐藏它。
    将您的结构重命名为其他名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-17
      • 1970-01-01
      • 2015-10-10
      • 2014-06-17
      • 2014-04-03
      • 1970-01-01
      • 2022-10-14
      相关资源
      最近更新 更多