Or if, like me, you don't want to do a ton of isfield()s on a deeply nested structure, you can shortcut to testing the last one like this:
eval('isfield(a.b.c.d.e.f,''g'')','0')
Of course, in the example I tried, this took 5 times longer than the multiple isfield method, so you have to decide if you need the speed or easier to read code. (Also, the deeper the nesting gets, the more efficient this version gets relative to multiple calls to isfield.) Furthermore, the eval() statement might also allow you to skip the 'if' entirely if you're just wanting to get the value of your nested field/assign a default value, e.g.
MyData = eval('a.b.c.d.e.f.g','NaN');
1 Comment
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/103924-is-it-possible-to-check-for-existence-of-fields-in-nested-structures-with-isfield-in-matlab-8-1-r20#comment_614222
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/103924-is-it-possible-to-check-for-existence-of-fields-in-nested-structures-with-isfield-in-matlab-8-1-r20#comment_614222
Sign in to comment.