Is there a MATLAB function that can check if a field exists in a MATLAB structure?
1,486 次查看(过去 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
2017-6-12
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
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 Center 和 File Exchange 中查找有关 Structures 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!