0001 function [parms, params_unspecified ] = dsCheckOptions(options, options_schema, strict)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033 if length(options) == 1 && ~isstruct(options) && isstruct(options{1})
0034 options = options{1};
0035 end
0036
0037 if ~isstruct(options) && (0 ~= mod(length(options),2))
0038 error('List of arguments must be even (must have name/value pair)');
0039 end
0040 if (0 ~= mod(length(options_schema),3))
0041 error('Programming error: list of default arguments must be even (must have name/value pair)');
0042 end
0043 if (~exist('strict', 'var'))
0044 strict = true;
0045 end
0046
0047 parms = [];
0048
0049
0050
0051 if ~isstruct(options)
0052 for ind = 1:length(options)
0053 if iscell(options{ind})
0054 if isempty(options{ind})
0055 options{ind}=[];
0056 elseif ~iscell(options{ind}{1})
0057
0058 end
0059 end
0060 end
0061 else
0062 flds = fieldnames(options);
0063 for ind = 1:length(flds)
0064 fld = flds{ind};
0065 if iscell(options.(fld))
0066 if isempty(options.(fld))
0067 options.(fld)=[];
0068 elseif ~iscell(options.(fld){1})
0069
0070 end
0071 end
0072 end
0073 end
0074
0075 if ~isstruct(options)
0076 input_fields = options(1:2:end);
0077 else
0078 input_fields = fieldnames(options);
0079 end
0080 valid_fields = options_schema(1:3:end);
0081 unknown_fields = setdiff(input_fields, valid_fields);
0082
0083
0084 if (strict && ~isempty(unknown_fields))
0085 error('The following unrecogized options were passed in: %s', sprintf('%s ',unknown_fields{:}));
0086 end
0087
0088 if ~isstruct(options)
0089 if (~isempty( options ))
0090 for f=1:length(options)/2
0091 parms.(options{2*f-1})=options{2*f};
0092 end
0093
0094 end
0095 else
0096 parms = options;
0097 end
0098
0099
0100
0101
0102
0103 params_unspecified = struct;
0104 if (~strict && ~isempty(parms) && ~isempty(options_schema))
0105 for i = 1:length(unknown_fields)
0106 params_unspecified.(unknown_fields{i}) = parms.(unknown_fields{i});
0107 end
0108 parms = rmfield(parms,unknown_fields);
0109 end
0110
0111
0112 for f=1:3:length(options_schema)
0113
0114
0115 if (isfield(parms,options_schema{f}))
0116 param_name = options_schema{f};
0117 param_value = getfield(parms,param_name);
0118 param_range = options_schema{f+2};
0119
0120
0121 if isempty(param_value)
0122 parms = setfield(parms, options_schema{f}, options_schema{f+1});
0123
0124 elseif isempty(param_range)
0125 continue;
0126
0127 elseif iscell(param_range)
0128 num_flag = 0;
0129 char_flag = 0;
0130
0131 for i=1:length(param_range)
0132 if isnumeric(param_range{i}), num_flag=1; end
0133 if ischar(param_range{i}), char_flag=1; end
0134 end
0135
0136 if num_flag && char_flag
0137 error('type of parameter range (cell array of numbers and strings) specified for parameter ''%s'' is currently unsupported', options_schema{f});
0138 elseif char_flag
0139 if iscell(param_value)
0140 for i=1:length(param_value)
0141 if ~ischar(param_value{i})
0142 error('parameter ''%s'' must be string or cell array of strings', options_schema{f});
0143 end
0144 end
0145 elseif ~ischar(param_value)
0146 error('parameter ''%s'' must be string or cell array of strings', options_schema{f});
0147 else
0148 param_value = {param_value};
0149 end
0150
0151 if length(find(ismember(param_value,param_range))) ~= length(param_value)
0152 error('parameter ''%s'' value must be one of the following: { %s}', ...
0153 param_name, sprintf('''%s'' ',param_range{:}));
0154 end
0155 elseif num_flag
0156 param_range = cell2mat(param_range);
0157
0158 if ~isnumeric(param_value)
0159 error('parameter ''%s'' must be numeric', options_schema{f});
0160 end
0161
0162 if length(find(ismember(param_value,param_range))) ~= length(param_value)
0163 error('parameter ''%s'' value must be one of the following: { %s}', ...
0164 param_name,sprintf('%d ',param_range));
0165 end
0166 else
0167 error('type of parameter range specified for parameter ''%s'' is currently unsupported', options_schema{f});
0168 end
0169
0170 elseif islogical(param_range) && length(param_range)==2
0171 if ~ismember(param_value,param_range)
0172 error('parameter %s value must be true (1) or false (0)', options_schema{f});
0173 end
0174
0175 elseif isnumeric(param_range) && length(param_range)==2
0176
0177 if ~isempty(find(param_value < param_range(1))) || ...
0178 ~isempty(find(param_value > param_range(2)))
0179 if int64(param_range(1))==param_range(1) && int64(param_range(2))==param_range(2)
0180 error('parameter %s value must be between %d and %d',...
0181 options_schema{f},param_range(1),param_range(2));
0182 else
0183 error('parameter %s value must be between %0.4f and %0.4f',...
0184 options_schema{f},param_range(1),param_range(2));
0185 end
0186 end
0187
0188 elseif isnumeric(param_range)
0189 if ~isnumeric(param_value)
0190 error('parameter ''%s'' must be numeric', options_schema{f});
0191 end
0192
0193 if length(find(ismember(param_value,param_range))) ~= length(param_value)
0194 error('parameter ''%s'' value must be one of the following: [ %s]', ...
0195 param_name,sprintf('%d ',param_range));
0196 end
0197
0198 else
0199 error('type of parameter range specified for parameter ''%s'' is currently unsupported', options_schema{f});
0200 end
0201
0202 else
0203
0204 parms.(options_schema{f})=options_schema{f+1};
0205 end
0206 end