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!