Home > database > ensemble_check_param.m

ensemble_check_param

PURPOSE ^

Checks for the existance of checkParam within params.

SYNOPSIS ^

function params = ensemble_check_param(checkParam,params,default)

DESCRIPTION ^

 Checks for the existance of checkParam within params.

 ensemble_check_param(checkParam,params,default)

 sees if checkParam, passed in as a string, is set in the params
 structure. Each param structure 'level' will be traversed and
 checked for existence.

 For example, if checkParam = 'params.test1.test2.test3' and
 params.test1 exists but not the other fields, this function will
 traverse each level (e.g. params.test1.test2 = [], then
 params.test1.test2.test3 = []. Only fields that don't exist yet are
 initialized and set to null.

 If a default value is given and the field wasn't already assigned
 a value, then the default value will be assigned to the
 last level (e.g. params.test1.test2.test3 = 4 if the default was
 4)

 if one of the lower params levels exists and is equal to a value, then
 the function will stop processing and return an error, instead of
 overwriting that value with a field (e.g. if params.test1 = 2,
 then the function will not attempt to overwrite it with
 params.test1.test2 = [])

 This function was primarily motivated by the desire to avoid lots
 of if-then-else statements when checking for parameter
 default values in ensemble functions.

 30 March 2007 - Stefan Tomic

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function params = ensemble_check_param(checkParam,params,default)
0002 % Checks for the existance of checkParam within params.
0003 %
0004 % ensemble_check_param(checkParam,params,default)
0005 %
0006 % sees if checkParam, passed in as a string, is set in the params
0007 % structure. Each param structure 'level' will be traversed and
0008 % checked for existence.
0009 %
0010 % For example, if checkParam = 'params.test1.test2.test3' and
0011 % params.test1 exists but not the other fields, this function will
0012 % traverse each level (e.g. params.test1.test2 = [], then
0013 % params.test1.test2.test3 = []. Only fields that don't exist yet are
0014 % initialized and set to null.
0015 %
0016 % If a default value is given and the field wasn't already assigned
0017 % a value, then the default value will be assigned to the
0018 % last level (e.g. params.test1.test2.test3 = 4 if the default was
0019 % 4)
0020 %
0021 % if one of the lower params levels exists and is equal to a value, then
0022 % the function will stop processing and return an error, instead of
0023 % overwriting that value with a field (e.g. if params.test1 = 2,
0024 % then the function will not attempt to overwrite it with
0025 % params.test1.test2 = [])
0026 %
0027 % This function was primarily motivated by the desire to avoid lots
0028 % of if-then-else statements when checking for parameter
0029 % default values in ensemble functions.
0030 %
0031 % 30 March 2007 - Stefan Tomic
0032 
0033 
0034 
0035 parm = regexp(checkParam,'\.(\w*[^\.])','tokens');
0036 nLevels = length(parm);
0037 
0038 %the string that will be used to check field existence
0039 compare = 'params';
0040 
0041 %iLevel refers to each param struct level
0042 for iLevel = 1:nLevels
0043 
0044   %see if the field is set for the next level
0045   eval(sprintf('paramExists = isfield(%s,parm{%d}{1});',compare,iLevel));
0046 
0047   %also see if the current level contains a value, in which case we
0048   %can't overwrite it with the field for the next level
0049   eval(sprintf('valueSet = ~isempty(%s) & ~isstruct(%s);', ...
0050            compare,compare));
0051     
0052   %throw an error if the current level contains a value
0053   if(valueSet)
0054     error(sprintf('%s contains a value so parameter checking cannot proceed',compare));
0055   end
0056 
0057   
0058   %if the field for the next level does not exist, then create it
0059   %and set it to empty
0060   if(~paramExists)
0061     eval(sprintf('%s.(parm{%d}{1}) = [];',compare,iLevel));
0062   end
0063   
0064   %append the field for the next level to the compare string
0065   compare = [compare sprintf('.%s',parm{iLevel}{1})];  
0066   
0067 end
0068 
0069 %now that we are at the last level, check if what we think should
0070 %be the last level actually contains a struct. If so, throw a
0071 %warning (since it's not empty, it will not be overwritten by the
0072 %following code)
0073 eval(sprintf('setToStruct = isstruct(%s);',compare));
0074 if(setToStruct)
0075   warning(sprintf('%s contains a structure. Parameter checking cannot proceed'));
0076 end
0077 
0078 %finally see if a value is already set at the last level
0079 eval(sprintf('valueSet = ~isempty(%s) & ~setToStruct;',compare));
0080 
0081 %if a value is not set at the last level and a default value has
0082 %been submitted, then set it to the default value
0083 if(exist('default','var') & ~valueSet)
0084   eval(sprintf('%s = default;',compare));
0085 end

Generated on Sun 25-Aug-2019 04:00:39 by m2html © 2003