Initialization of array of structure

3 visualizaciones (últimos 30 días)
Thulasi Durai Durai Samy
Thulasi Durai Durai Samy el 19 de Jul. de 2012
Hello
I have a created array of structure (named LD) which incrementes from 1 to no of database (eg LD(1) to LD(10)). The structure has some fields. After some time I need to re-initialise the structure.
I tried with following commands.
LD = [];
But, I lost the field names, how to initialise with out loosing the field names.

Respuesta aceptada

Walter Roberson
Walter Roberson el 19 de Jul. de 2012
Experiment with
LD = LD(false);
  4 comentarios
Walter Roberson
Walter Roberson el 20 de Jul. de 2012
Yes, that is fine.
Walter Roberson
Walter Roberson el 20 de Jul. de 2012
For the clearing, you might be able to use
LD(:) = [];
Unfortunately I cannot test that at the moment.

Iniciar sesión para comentar.

Más respuestas (2)

Nirmal
Nirmal el 19 de Jul. de 2012
You shouldnt do LD=[], instead you should change the field of each of the structure in the array.

Conrad
Conrad el 19 de Jul. de 2012
The following code should do the trick:
function is = StructInit(s)
is = s;
if isstruct(s)
f = fields(s);
for i = 1 : length(f);
if isstruct(is.(f{i}))
is.(f{i}) = StructInit(is.(f{i}));
else
is.(f{i}) = [];
end
end
else
is = [];
end
end
Here is an example how to use it:
% Create dummy structure.
a.a = 1;
a.b = '';
a.c = {};
a.d.a.a = 2;
a.d.a.b = 1;
a.d.b = {};
initialisedStruct = StructInit(a);
All the fields are initialised to []. If a field is a structure, that structure's fields will be initialised etc.
Conrad

Categorías

Más información sobre Matrix Indexing 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