Check if any field in a given structure is empty
315 views (last 30 days)
I have a structure with quite some fields and I want to check, if any of the fields are empty. Is there a way to do this without checking for each field individually?
Currently I have an if statement with many ORs to do this:
if isempty(structure.field1) || isempty(structure.field2)
and so on
I tried to do the same with
but obviously the fieldnames are not empty.
More Answers (2)
Ross Nye on 30 Nov 2017
I'm going to leave this here in case someone comes across this page looking for a similar answer to what I was.
I have a 1xN struct with many fields. One particular field was supposed to be boolean, but only the falses had been entered and the rest were . The above answers didn't seem to help me, but I did find something on Reddit which did.
emptyIndex = find(arrayfun(@(MyStruct) isempty(MyStruct.myField),MyStruct));
I can then use emptyIndex to set the fields to true as needed.
LeChat on 13 Aug 2020
It might be a bit dirty because I use a loop but it works:
Then ind contains all the indexes where the structure field is empty.
For instance if you then want to remove them from the structure:
Hope this helps!