Search code examples
matlabstructfieldcellis-empty

Check if Matlab struct, dynamic Field Value is empty


I have a struct x, with dynamic Fields, respectively dynamic Field names. But basically, only the first Field is relevant for me. So I want to check if the Value of the first Field is empty, speak a 1x1cell or a 0x1cell..

enter image description here

or

enter image description here

I'm experimenting e.g. with:

isempty(fieldnames(x))
isempty(x(1))

if isempty(x(1))
    msgbox('empty')
else
    msgbox('result')
end

but got to no solution. Does anybody have a clue?

Speak, check if the Value of the first Field of the struct is empty or not..


Solution

  • If only the first field is relevant to you, then you can proceed as follows :

    Get the fieldnames list of your struct

    names=fieldnames(x);
    

    Get the size of the first field

    SizeOfFirstField=size(x.(names{1}));
    

    Then you can just check if the first value in SizeOfFirstField is 0 or 1 in your if condition :

    if SizeOfFirstField(1)==0
        msgbox('empty')
    else
        msgbox('result')
    end