Borrar filtros
Borrar filtros

array of struct arrays with varying length

13 visualizaciones (últimos 30 días)
Greg
Greg el 18 de Mayo de 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."

Respuesta aceptada

Walter Roberson
Walter Roberson el 18 de Mayo de 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',[]) ];

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by