Anyway to Pre-allocate Memory for Struct

2 visualizaciones (últimos 30 días)
nas illmatic
nas illmatic el 17 de Jul. de 2015
Movida: Stephen23 el 14 de Dic. de 2022
Is there anyway to pre-allocate memory for a struct?
I have a
1x2 struct with 2 fields.
>> s = struct('Name',0,'Name2',0)
But, it comes up as a 1x1 struct.

Respuestas (3)

Walter Roberson
Walter Roberson el 17 de Jul. de 2015
s = struct('Name',{0,0},'Name2',{0,0});
That is, use a cell array, one entry per struct member.

prasanth s
prasanth s el 14 de Dic. de 2022
s=struct;
s(1).Name=0
s(2).Name=0

Stephen23
Stephen23 el 14 de Dic. de 2022
Movida: Stephen23 el 14 de Dic. de 2022
Here are two generalizable, easily expandable appraches:
S = repmat(struct('Name',[],'Name2',[]),1,2)
S = 1×2 struct array with fields:
Name Name2
C = cell(1,2);
S = struct('Name',C,'Name2',C)
S = 1×2 struct array with fields:
Name Name2

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by