array of struct arrays with varying length

13 views (last 30 days)
Ideally, my calculation needs to preallocate an array, myArray, (of predetermined length) in which each element is an array of structs -- an array which may grow in length during the course of the calculation. These arrays of structs must grow independently, so at the end of the calculation, the array of structs in myArray(1) may have a different length than myArray(2), etc.
The memory need not be contiguous, neither for myArray nor for the array of structs myArray(i).
Trying this
myArray(1:10) = { [ struct('index',int32(0), 'result',[]) ] };
% ... later, when an element needs to grow:
myArray(1) = { myArray(1); [ struct('index', int32(0),'result',[]) ] };
fails on that last line with the message, "In an assignment A(:) = B, the number of elements in A and B must be the same."

Accepted Answer

Walter Roberson
Walter Roberson on 18 May 2012
A cell array can hold a struct array in each location, but a struct array location cannot hold a struct array directly. A struct array indexed at an element must be a structure scalar. That structure scalar could have a single field which held a struct array, though.
myArray{1} = [myArray{1}; struct('index', int32(0),'result',[]) ];

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!