Search code examples
matlabstructcell

How to see the elements of a struct


I have some results like:

    KinDetail = 

        []    [1x12 struct]    [1x72 struct]    [1x432 struct]    [1x2592 struct]

    KinDetail{2}

    ans = 

    1x12 struct array with fields:

        Comp
        DHtab
        jt

KinDetail{2}.DHtab

ans =

         0   -1.5708         0    0.1000
         0         0         0    0.9500
.
.
.
.

ans =

         0   -1.5708         0    0.8500
         0         0         0    0.2000

I need all results for each element of the struct. When I write KinDetail{2}.DHtab(1) Iexpect to see:

KinDetail{2}.DHtab(1)=

         0   -1.5708         0    0.1000
         0         0         0    0.9500

but it gives error and says:

Expected one output from a curly brace or dot indexing expression, but there were 12 results.

How can I get each result individually.

Thanks in advance.


Solution

  • You've placed the (1) in the wrong place. Try

    KinDetail{2}(1).DHtab
    

    instead.

    Explanation

    Calling KinDetail{2}.DHtab(1) attempts to get the first value in kinDetail{2}.DHtabwhich is not allowed in heterogeneous data structures.

    On the other hand to get the first element of the struct in cell 2 of KinDetail we can call KinDetail{2}(1) which then lets us look at the DHtab value

    KinDetail{2}(1).DHtab
    

    Furthermore, if you actually wanted to access the first value of each element's DHtab you could implement arrayfun as such:

    arrayfun(@(st)st.DHtab(1),KinDetail{2})