【问题标题】:cell array of struct-Files结构文件的元胞数组
【发布时间】:2012-07-12 00:59:51
【问题描述】:

我创建了一个结构文件的元胞数组,例如:

>> res2

res2 = 

  Columns 1 through 7

    [1x1 struct]    [1x1 struct]    [1x1 struct]    [1x1 struct]    [1x1 struct]    [1x1 struct]    [1x1 struct]

  Columns 8 through 10

    [1x1 struct]    [1x1 struct]    [1x1 struct]



>> res2{1}

ans = 

    nchi005_randchi005: 0.1061
          nfdr_randfdr: 0.0011
          nlgt_randlgt: 2.9517e-004
      nphast_randphast: 0.6660
           ndd_rand_dd: 0.0020
    ndd_rand_dd_larger: 1

    >> res2{1}.nlgt_randlgt

ans =

  2.9517e-004


>> res{:}.nlgt_randlgt
??? Bad cell reference operation.

是否可以一次访问 res2-cellarray 的所有 nlgt_randlgt-fields?

【问题讨论】:

  • 根据我对 Matlab 中数据组织方式的理解...否

标签: matlab struct cell


【解决方案1】:

您需要做的就是将您的res2 从元胞数组转换为结构数组(使用cell2mat)。然后,您可以完全按照您想要的方式获取结构成员。下面是一个示例,其中cdat 是具有两个成员的结构元胞数组,s1s2

cdat = 

    [1x1 struct]    [1x1 struct]    [1x1 struct]    [1x1 struct]    [1x1 struct]    [1x1 struct]    [1x1 struct]    [1x1 struct]    [1x1 struct]    [1x1 struct]

>> dat = cell2mat(cdat)

dat = 

1x10 struct array with fields:
    s1
    s2

>> [dat(:).s1]

ans =

     1     1     1     1     1     1     1     1     1     1

【讨论】:

    【解决方案2】:

    您可以通过以下方式访问该单元格:

    cellfun(@(r) r.nlgt_randlgt, res2);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多