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

조회 수: 1,617 (최근 30일)
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

채택된 답변

MathWorks Support Team
MathWorks Support Team 대략 13시간 전
편집: MathWorks Support Team 대략 6시간 전
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
  댓글 수: 1
broken_arrow
broken_arrow 2021년 9월 19일
편집: broken_arrow 2021년 9월 19일
I modified it a little to also work with multilevel fieldName inputs like 'b.c'. I too second extending the built in isfield accordingly.
function out_isfieldresult = anyisfield(in_rootstruct,in_field)
%ANYISFIELD: Extension of built in isfield function. Added features:
%1. Multilevel inputs (e.g. "b.c.d")
%2. Identification of matches at any level of nested structs
in_field = string(in_field);
rootfieldnames = string(fieldnames(in_rootstruct(1)));
out_isfieldresult = false;
if contains(in_field,".")
current_searchfieldname = extractBefore(in_field,".");
else
current_searchfieldname = in_field;
end
remaining_searchfieldnames = extractAfter(in_field,".");
for i=1:length(rootfieldnames)
if strcmp(rootfieldnames(i),current_searchfieldname)
if ismissing(remaining_searchfieldnames)
out_isfieldresult = true;
return
else
out_isfieldresult = anyisfield(in_rootstruct(1).(current_searchfieldname),remaining_searchfieldnames);
end
elseif isstruct(in_rootstruct(1).(rootfieldnames(i)))
out_isfieldresult = anyisfield(in_rootstruct(1).(rootfieldnames(i)),current_searchfieldname);
if out_isfieldresult == true
return;
end
end
end
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by