Problem with using isfield

조회 수: 3 (최근 30일)
Veronika
Veronika 2017년 4월 6일
답변: Jan 2017년 4월 6일
Dear all,
I have one structure with 4 fields and I would like to use function isfield for other work.
I have this part of code:
if isfield(shape,'leftLung',shape,'rightLung',shape,'spine');
tmp2 = shape.spine{1};
tmp3 = shape.leftLung{1};
tmp4 = shape.rightLung{1};
and I have this error:
Error using isfield
Too many input arguments.
Error in Newmodel (line 115)
if isfield(shape,'leftLung',shape,'rightLung',shape,'spine');
Can you please advise me, how to change this part of code for good function? Thank you for your answers.
  댓글 수: 1
Jan
Jan 2017년 4월 6일
The shown code has no valid Matlab syntax. Therefore the readers cannot know its intention. Suggesting a wrong idea might be more confusing then useful for you.

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

답변 (2개)

Thorsten
Thorsten 2017년 4월 6일
편집: Thorsten 2017년 4월 6일
if isfield(shape,'leftLung')
tmp3 = shape.leftLung{1};
end
if isfield(shape, 'rightLung')
tmp4 = shape.rightLung{1};
end
if isfield(shape,'spine');
tmp2 = shape.spine{1};
end
  댓글 수: 1
Steven Lord
Steven Lord 2017년 4월 6일
You're probably going to want to add else blocks to each of those if statements, to assign default values to tmp3, tmp4, and tmp2 if the shape struct doesn't contain fields by those names.

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


Jan
Jan 2017년 4월 6일
If you have a structure with 4 fields, checking the existing of fields is not necessary. I guess:
if isfield(shape,'leftLung') && isfield(shape,'rightLung') && isfield(shape,'spine')
tmp2 = shape.spine{1};
tmp3 = shape.leftLung{1};
tmp4 = shape.rightLung{1};
end

카테고리

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