Search code examples
arraysmatlabstructcell-array

Creating a list of variable length arrays in MATLAB


I would like to process a list of images with regionprops and save the result into an array or a list of some kind.

My problem is that regionprops returns a variable size struct. It looks like this in my case:

stats = regionprops(L,'Centroid');

2x1 struct array with fields:
    Centroid

The size of the struct depends from image to image.

How can I create an object which holds all my structs? Do I need to use cell arrays?

I tried the following:

mycell = struct2cell(stats);
centers(i,:) = mycell;

But it only works as long as my cell arrays have the same size. It is not the case here, as the number of detected objects change from frame to frame.

How can I store variable length structs or cell arrays in a container?

What should I use, structs of cell arrays?


Solution

  • Just put each struct array stats inside a cell of its own. For example:

    >> stats1(3).test = 1
    
    stats1 = 
    
    1x3 struct array with fields:
        test
    
    >> stats2(2).test = 1
    
    stats2 = 
    
    1x2 struct array with fields:
        test
    
    >> [{stats1} {stats2}]
    
    ans = 
    
        [1x3 struct]    [1x2 struct]