Effacer les filtres
Effacer les filtres

array of struct arrays with varying length

6 vues (au cours des 30 derniers jours)
Greg
Greg le 18 Mai 2012
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."

Réponse acceptée

Walter Roberson
Walter Roberson le 18 Mai 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',[]) ];

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by