Initialization of array of structure

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.

 채택된 답변

Walter Roberson
Walter Roberson 2012년 7월 19일

0 개 추천

Experiment with
LD = LD(false);

댓글 수: 4

Hello Walter, many thanks for your help!!
It works perfect to re-initialise whole set of database. If I want to delete particular database in-between, how to modify the above code.
for example LD(1)-LD(10) array of structure exists,
remove LD(4), it has
LD(4).field1
LD(4).field2
LD(4).field3
LD(4).field4
so that my final database size is LD(1) - LD(9)
Hi Walter
I think LD(4) = [] should work, please correct me if I am wrong
Walter Roberson
Walter Roberson 2012년 7월 20일
Yes, that is fine.
For the clearing, you might be able to use
LD(:) = [];
Unfortunately I cannot test that at the moment.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Nirmal
Nirmal 2012년 7월 19일

0 개 추천

You shouldnt do LD=[], instead you should change the field of each of the structure in the array.
Conrad
Conrad 2012년 7월 19일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by