File Exchange

image thumbnail

isempty_field - Easily check whether a deeply nested field is empty or non-existent

version 1.0 (4.07 KB) by Terence Brouns
This function checks if a field is empty/non-existent without needing its parent fields to exist

1 Download

Updated 03 Jul 2018

View License

tf = isempty_field(S,fname)

This function checks whether a field is empty/non-existent. It is particularly useful when we want to check whether a
deeply nested field is empty, but are unsure if the field and/or its parent fields exist. The functions "isempty" and "isfield" cannot readily be applied for this purpose, because they require the parent fields to exist.

For example, we have a field "S.a.b.c.d.e.f" and want to check whether it is empty or not, but are not sure whether all of its parent fields ("a.b.c.d.e") exist. We could use "isfield" on each of the parent fields, but this is quite a hassle and requires several lines of code. Instead we can simply call:

tf = isempty_field(S,'S.a.b.c.d.e.f');

The function will return false when the field "S.a.b.c.d.e.f" exists AND is not empty, and true otherwise.

Cite As

Terence Brouns (2021). isempty_field - Easily check whether a deeply nested field is empty or non-existent (https://www.mathworks.com/matlabcentral/fileexchange/67918-isempty_field-easily-check-whether-a-deeply-nested-field-is-empty-or-non-existent), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (1)

Chang hsiung

quite handy, thanks a lot

MATLAB Release Compatibility
Created with R2011b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired by: Get structure field names in recursive manner

Community Treasure Hunt

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

Start Hunting!