-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
iFunc: better field search in isfield
- Loading branch information
Showing
1 changed file
with
24 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,39 @@ | ||
function b = isfield(a, field) | ||
function [b, f] = isfield(a, field) | ||
% b = isfield(s, field) : check existence of field/parameter in model objects | ||
% | ||
% @iFunc/isfield function which checks if a name is already defined as a Parameter | ||
% isfield(s) returns true when the field is defined in the object. | ||
% isfield(s, field) returns true when the field is defined in the object. | ||
% | ||
% [b, match] = isfield(s, field) | ||
% also returns the field match, which value is: get(s, match) | ||
% | ||
% A wider/nested search is obtained with: findfield(s, field) | ||
% | ||
% input: s: object or array (iFunc) | ||
% field: name to check for (string) | ||
% output: b: not empty when the parameter is already defined, false otherwise | ||
% ex: b=isfield(a, 'Temperature'); | ||
% output: b: true when the parameter is already defined, false otherwise | ||
% ex: b=isfield(bose, 'Temperature'); | ||
% | ||
% Version: $Date$ | ||
% See also iFunc, isfield, iFunc/findfield | ||
|
||
if nargin == 1, field=[]; end | ||
b=false; f=[]; | ||
|
||
if isempty(field), b=a.Parameters; return; end | ||
|
||
b = any(strcmp(field, a.Parameters)); | ||
for fields={a.Parameters, fieldnames(a)} | ||
this_fields = fields{1}; | ||
match = strcmp(field, this_fields); % search first for exact match | ||
|
||
if ~any(match) | ||
match = strcmp(lower(field), lower(strtok(this_fields))); % relaxed search (case insensitive) | ||
end | ||
|
||
if any(match), | ||
b = find(match, 1, 'first'); | ||
f = strtok(this_fields{b}); | ||
break | ||
end | ||
end | ||
|