Is it possible to check for existence of fields in nested structures with isfield in MATLAB 8.1 (R2013a)?
조회 수: 38 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2013년 10월 25일
답변: Steven Lord
2022년 12월 16일
I have the following structure
a.b.c = 1;
I know it is possible to search for the nested
isfield(a.b,'c')
But I would like to check for the existence of the field 'c' even when I am not sure that the field 'b' exists, e.g.
isfield(a,'b.c');
채택된 답변
MathWorks Support Team
2013년 10월 25일
There is no MATLAB function to determine the existence of fields in nested structures. The only workaround is to check separately for the existence of 'b' and of 'c':
isfield(a, 'b') && isfield(a.b, 'c')
댓글 수: 0
추가 답변 (1개)
Steven Lord
2022년 12월 16일
Another approach that uses neither eval nor repeated calls to isfield is to use getfield.
a.b.c = 1;
isNestedField(a, 'b.c')
isNestedField(a, 'b.d')
isNestedField(1:10, 'a')
function tf = isNestedField(s, thefields)
fieldlist = split(thefields, '.');
try
getfield(s, fieldlist{:});
tf = true;
catch
tf = false;
end
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!