Search code examples
matlabmatlab-struct

Convert Cell to Struct in Matlab


I'm trying to access contents of a cell array which has 4 structures.

celldisp(tracks_array);

gives output:

tracks_array{1} =
             kalmanFilter: [1×1 vision.KalmanFilter]
                       id: 0
        totalVisibleCount: 1
                     bbox: [390 171 70 39]
consecutiveInvisibleCount: 0
                      age: 1
tracks_array{2} =
             kalmanFilter: [1×1 vision.KalmanFilter]
                       id: 1
        totalVisibleCount: 1
                     bbox: [459 175 40 24]
consecutiveInvisibleCount: 0
                      age: 1
tracks_array{3} =
             kalmanFilter: [1×1 vision.KalmanFilter]
                       id: 2
        totalVisibleCount: 1
                     bbox: [220 156 159 91]
consecutiveInvisibleCount: 0
                      age: 1
tracks_array{4} =
             kalmanFilter: [1×1 vision.KalmanFilter]
                       id: 3
        totalVisibleCount: 1
                     bbox: [510 159 68 49]
consecutiveInvisibleCount: 0
                      age: 1

Then I Use a for loop to iterate through the elements..

for elmen = tracks_array
 structtt=cell2struct(elmen(1),{'id','bbox','kalmanFilter','age','totalVisibleCount','consecutiveInvisibleCount'},2);

This gives error of

Error using cell2struct
Number of field names must match number of fields in new structure.

Then I used this inside the for loop

disp(elmen)
celldisp(elmen)

gives,

[1×1 struct]
 elmen{1} =
             kalmanFilter: [1×1 vision.KalmanFilter]
        totalVisibleCount: 1
                     bbox: [390 171 70 39]
 consecutiveInvisibleCount: 0
                       id: 0
                      age: 1

I want to access the elements by their field names. How do I do this?

Right now if I try to use getfield it gives this error:

Struct contents reference from a non-struct array object.

Solution

  • There is an oddity in Matlab when using for to iterate through cell arrays. You would expect it to give you the actual element on each iteration, but instead it gives you a cell array containing just one value, which is that element. But it's easy to extract the actual element using elmen{1}. So in your code example:

    for elmen = tracks_array
    
        % The actual element is the first and only entry in the cell array
        structtt = elmen{1};
    
        % Display the structure
        disp(structtt);
    
        % Display a field within the structure
        disp(structtt.totalVisibleCount);
    
        % The same as above, but using getfield()
        disp(getfield(structtt, 'totalVisibleCount'));
    end
    

    You can also write the above as a for loop over the cell array, using the {} syntax to extract each element

    for index = 1 : length(tracks_array)
        structtt = tracks_array{index};
    
        % Display the structure
        disp(structtt);
    
        % Display a field within the structure
        disp(structtt.totalVisibleCount);
    
        % The same as above, but using getfield()
        disp(getfield(structtt, 'totalVisibleCount'));
    end