Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cell array of struct-Files

I have created a cell array of structure-files, like this for example:

>> 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.

Is there a possiblity to access to all nlgt_randlgt-fields of res2-cellarray at once?

like image 808
Hakan Kiyar Avatar asked Jul 12 '12 00:07

Hakan Kiyar


2 Answers

All you need to do is convert your res2 from a cell array to a struct array (using cell2mat). Then you can get at the struct members in exactly the manner you desire. Here is an example, where cdat is a cell array of structs with two members, s1 and s2.

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
like image 176
Kaelin Colclasure Avatar answered Sep 21 '22 16:09

Kaelin Colclasure


you can access to the cell by:

cellfun(@(r) r.nlgt_randlgt, res2);
like image 35
Serg Avatar answered Sep 21 '22 16:09

Serg