I have a structure within a structure (a nested structure), and am trying to add a field to that structure, what should I write?

조회 수: 1 (최근 30일)
s.Field.(FieldName)=zeros(1,1);
Doesn't work...

답변 (1개)

Fabian Schadt
Fabian Schadt 2017년 3월 23일
편집: Stephen23 2017년 3월 23일
Hi Guy Nir,
actually there should be no problem.
s.Field.FieldName = zeros(1,1);
works ;-)
However, if you state your nested struct with brackets ->
s.Field.(FieldName),
then you access with (FieldName) a variable in your workspace. If this does not exist, you receive an error.
If brackets, then it would be like this:
FieldName = [];
s.Field.(FieldName) = zeros(1,1);
Hope I could help!
  댓글 수: 5
James Tursa
James Tursa 2017년 3월 23일
편집: James Tursa 2017년 3월 23일
Works for me:
>> clear all
>> FieldName='valid'
FieldName =
valid
>> s.Field.(FieldName) = zeros(1,3)
s =
Field: [1x1 struct]
>> s.Field.valid
ans =
0 0 0
You should double check what the variable s really is. Are you sure it isn't maybe a cell array that contains structures, or something like that?
Guy Nir
Guy Nir 2017년 3월 23일
Thanks. It seems that because I had a 2X1 structure within a structure, it was a bit more complicated. but i got it done thanks to all of you!
fluidicsSeq{1,1}.ClustData(1).(FieldName)=zeros(1,3)

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

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by