check if field exists in a structure in matlab

Use the isfield function to check if a field exists in a structure in Matlab. isfield(structure, field) accepts a structure and a string as input arguments and returns a logical indicating if the field exists in the structure or not. Here is an example:

main.m
mystruct = struct('field1', 1, 'field2', 'hello world');
if isfield(mystruct, 'field1')
   disp('field1 exists in the structure');
else
    disp('field1 does not exist in the structure');
end
192 chars
7 lines

This will output: "field1 exists in the structure".

related categories

gistlibby LogSnag