MATLAB Answers

Is there a MATLAB function that can check if a field exists in a MATLAB structure?

3,004 views (last 30 days)
The 'exist' function returns true if a structure with a particular name exists. The 'isfield' function returns true if a field is in a structure array. However, I would like a function that determines if a field exists anywhere in a structure of structures. For example, in the following code, the 'isfield' function does not identify that "c" is a field of "a".
a.b.c = 1;
isfield(a, 'c')
results in:
ans =
0

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 12 Jun 2017
There is no MATLAB function that examines every level of a structure of structures, or nested structure, to determine if a field exists. The 'isfield' function examines only the top level of a nested structure. To determine if a field exists at any other level, you can use either of the following methods.
1. To determine if a field exists in a particular substructure, use 'isfield' on that substructure instead of the top level. In the example, the value of a.b is itself a structure, and you can call 'isfield' on it.
a.b.c = 1;
isfield(a.b,'c')
ans =
logical
1
Note: If the first input argument is not a structure array, then 'isfield' returns 0 (false). While there are other MATLAB data types that have properties you can access with dot notation, those other data types are not structure arrays.
2. To determine if a field exists at any level in a nested structure, create a new function that examines all levels of the structure. Open the MATLAB Editor and paste the following function into it. Save the function as a MATLAB file, named 'myIsField.m'.
function isFieldResult = myIsField (inStruct, fieldName)
% inStruct is the name of the structure or an array of structures to search
% fieldName is the name of the field for which the function searches
isFieldResult = 0;
f = fieldnames(inStruct(1));
for i=1:length(f)
if(strcmp(f{i},strtrim(fieldName)))
isFieldResult = 1;
return;
elseif isstruct(inStruct(1).(f{i}))
isFieldResult = myIsField(inStruct(1).(f{i}), fieldName);
if isFieldResult
return;
end
end
end
Call 'myIsField' on a nested structure.
a.b.c = 1;
myIsField(a,'c')
ans =
logical
1

  4 Comments

Show 1 older comment
Adam Halasz
Adam Halasz on 8 Aug 2020
The perfect solution would be a seamless extension to exist(), that would interpret the input string as if you used it to identify a variable. Return a logical 1 or 0 if that field is defined or not.
clear all
a.b.c = 1;
exist('a.b')
ans =
logical
1
exist('a.b.c')
ans = 1
exist('u.b')
ans = 0
exsit('a.c')
ans = 0

Sign in to comment.

More Answers (3)

Bill Connelly
Bill Connelly on 30 Oct 2015
Just in case someone comes along this thread in future, you can achieve the same thing in one line
if sum(strcmp(fieldnames(STRUCTURE), 'FIELDNAME')) == 1
%do something
end

  6 Comments

Show 3 older comments
RG
RG on 5 Feb 2019
@Bill Connely, checked your code the sum always equal 0...
Using the above posted myIsField function file - works for me.
Thanks for the efforts....
Walter Roberson
Walter Roberson on 6 Feb 2019
videoinput is an object not a structure so it can have properties that look like fieldnames and it can have nonpublic fields.

Sign in to comment.


Andrew Miller
Andrew Miller on 19 May 2017
I required the same solution to this problem and decided to upload it to Matlab Central. Hope it helps!
The premise is: breadth-first search to find any occurrence and bug-out on the first one, returning the number of levels down it occurred treating S as a tree of struct-nodes.

  0 Comments

Sign in to comment.


Zhe Li
Zhe Li on 19 Oct 2017
One (simple) workaround is to use try-catch structure and look for MATLAB:nonExistentField error identifier.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!