-
Notifications
You must be signed in to change notification settings - Fork 1
/
CheckParam.m
253 lines (219 loc) · 11.3 KB
/
CheckParam.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
classdef CheckParam
methods (Static)
%Type checking
%Numeric (double is the default matlab type for all numeric data)
function confirm = isNumeric(inputParameter, errorIdentifier, errorMessage)
if(~exist('errorMessage','var')) %if we didn't pass in a specific error message...
errorMessage = 'Parameter must be a numeric value'; %...use a generic one
end
exception = MException(errorIdentifier,errorMessage);
if(strcmp(class(inputParameter),'ScriptVariable'))
numIterations = length(inputParameter.valueArray);
if(numIterations == 0) %if empty
throwAsCaller(exception);
else
for i=1:numIterations
CheckParam.isNumeric(inputParameter.valueArray{i}, errorIdentifier, errorMessage);
end
end
elseif(isempty(inputParameter))
throwAsCaller(exception);
elseif(~strcmp(class(inputParameter),'double'))
throwAsCaller(exception);
end
confirm = true;
end
%Integer - NOTE that the default matlab type for all numeric data,
%integer and floating point is double. So, we test for
%divisibility by 1 to determine if it is an integer
function confirm = isInteger(inputParameter, errorIdentifier, errorMessage)
if(~exist('errorMessage','var')) %if we didn't pass in a specific error message...
errorMessage = 'Parameter must be an integer value'; %...use a generic one
end
exception = MException(errorIdentifier,errorMessage);
if(strcmp(class(inputParameter),'ScriptVariable'))
numIterations = length(inputParameter.valueArray);
if(numIterations == 0) %if empty
throwAsCaller(exception);
else
for i=1:numIterations
CheckParam.isInteger(inputParameter.valueArray{i}, errorIdentifier, errorMessage);
end
end
else
try %make sure the parameter is at least numeric
CheckParam.isNumeric(inputParameter, 'CheckParam:isInteger:badInputs');
catch err
throwAsCaller(exception);
end
%now that we know it is numeric, check for divisibility by one
if(mod(inputParameter, 1))
throwAsCaller(exception);
end
end
confirm = true;
end
%String
function confirm = isString(inputParameter, errorIdentifier, errorMessage)
if(~exist('errorMessage','var')) %if we didn't pass in a specific error message...
errorMessage = 'Parameter must be a string value'; %...use a generic one
end
exception = MException(errorIdentifier,errorMessage);
if(strcmp(class(inputParameter),'ScriptVariable'))
numIterations = length(inputParameter.valueArray);
if(numIterations == 0) %if empty
throwAsCaller(exception);
else
for i=1:numIterations
CheckParam.isString(inputParameter.valueArray{i}, errorIdentifier, errorMessage);
end
end
elseif(~ischar(inputParameter))
throwAsCaller(exception);
end
confirm = true;
end
%Char
function confirm = isChar(inputParameter, errorIdentifier, errorMessage)
if(~exist('errorMessage','var')) %if we didn't pass in a specific error message...
errorMessage = 'Parameter must be a char value'; %...use a generic one
end
exception = MException(errorIdentifier,errorMessage);
if(strcmp(class(inputParameter),'ScriptVariable'))
numIterations = length(inputParameter.valueArray);
if(numIterations == 0) %if empty
throwAsCaller(exception);
else
for i=1:numIterations
CheckParam.isChar(inputParameter.valueArray{i}, errorIdentifier, errorMessage);
end
end
else
try %make sure the parameter is at least a string
CheckParam.isString(inputParameter, 'CheckParam:isInteger:badInputs');
catch err
throwAsCaller(exception);
end
%now that we know it is a string, check for a length of one
if(length(inputParameter)~=1)
throwAsCaller(exception);
end
end
confirm = true;
end
%Cell array
function confirm = isCellArray(inputParameter, errorIdentifier, errorMessage)
if(~exist('errorMessage','var')) %if we didn't pass in a specific error message...
errorMessage = 'Parameter must be a cell array'; %...use a generic one
end
if(~iscell(inputParameter))
exception = MException(errorIdentifier,errorMessage);
throwAsCaller(exception);
end
confirm = true;
end
%Cell array of strings
function confirm = isCellArrayOfStrings(inputParameter, errorIdentifier, errorMessage)
if(~exist('errorMessage','var')) %if we didn't pass in a specific error message...
errorMessage = 'Parameter must be a cell array of strings'; %...use a generic one
end
if(~iscellstr(inputParameter))
exception = MException(errorIdentifier,errorMessage);
throwAsCaller(exception);
end
confirm = true;
end
%Boolean
function confirm = isBoolean(inputParameter, errorIdentifier, errorMessage)
if(~exist('errorMessage','var')) %if we didn't pass in a specific error message...
errorMessage = 'Parameter must be a boolean variable'; %...use a generic one
end
exception = MException(errorIdentifier,errorMessage);
if(strcmp(class(inputParameter),'ScriptVariable'))
numIterations = length(inputParameter.valueArray);
if(numIterations == 0) %if empty
throwAsCaller(exception);
else
for i=1:numIterations
CheckParam.isBoolean(inputParameter.valueArray{i}, errorIdentifier, errorMessage);
end
end
elseif(~isa(inputParameter, 'logical'))
throwAsCaller(exception);
end
confirm = true;
end
%Any particular class type
function confirm = isClassType(inputParameter, className, errorIdentifier, errorMessage)
CheckParam.isString(className, 'CheckParam:isClassType:badInputs');
if(~exist('errorMessage','var')) %if we didn't pass in a specific error message...
errorMessage = 'Parameter must be of type ';
errorMessage = strcat(errorMessage, className); %...use a generic one
end
if(~strcmp(class(inputParameter),className))
exception = MException(errorIdentifier,errorMessage);
throwAsCaller(exception);
end
confirm = true;
end
%Value checking (do only after type checking)
%Check to make sure the parameter value is within a range
function confirm = isWithinARange(param, lowerBound, upperBound, errorIdentifier, errorMessage)
CheckParam.isNumeric(param, 'CheckParam:isWithinARange:badInputs');
CheckParam.isNumeric(lowerBound, 'CheckParam:isWithinARange:badInputs');
CheckParam.isNumeric(upperBound, 'CheckParam:isWithinARange:badInputs');
if(lowerBound > upperBound) %...if the bounding conditions are reversed
temp = lowerBound; %switch
lowerBound = upperBound;
upperBound = temp;
end
if(~exist('errorMessage','var')) %if we didn't pass in a specific error message...
errorMessage = sprintf('Parameter must be between %f and %f', lowerBound, upperBound); %...use a generic one
end
if((param < lowerBound) || (param > upperBound))
exception = MException(errorIdentifier,errorMessage);
throwAsCaller(exception);
end
confirm = true;
end
%Check to make sure the parameter is one of a short, finite list of acceptable values:
function confirm = isInList(param, list, errorIdentifier, errorMessage)
CheckParam.isCellArrayOfStrings(list, 'CheckParam:isInList:badInputs');
if(~exist('errorMessage','var')) %if we didn't pass in a specific error message...
errorMessage = 'Paramter value is not on the list of acceptable values'; %...use a generic one
end
exception = MException(errorIdentifier, errorMessage);
if(strcmp(class(param),'ScriptVariable'))
numIterations = length(param.valueArray)
if(numIterations == 0) %if empty
throwAsCaller(exception);
else
for i=1:numIterations
CheckParam.isInList(param.valueArray{i}, list, errorIdentifier, errorMessage);
end
end
else
CheckParam.isString(param, 'CheckParam:isInList:badInputs');
%param = param
if(~ismember(param, list))
throwAsCaller(exception);
end
end
confirm = true;
end
%Scan string input to make sure it adheres to a particular format and return the parameter values:
function params = scanFormattedInput(inputString, formatString, numExpectedParams, errorIdentifier, errorMessage)
CheckParam.isString(inputString, 'CheckParam:scanFormattedInput:badInputs');
CheckParam.isString(formatString, 'CheckParam:scanFormattedInput:badInputs');
CheckParam.isInteger(numExpectedParams, 'CheckParam:scanFormattedInput:badInputs');
if(~exist('errorMessage','var')) %if we didn't pass in a specific error message...
errorMessage = 'Input string could not be correctly parsed'; %...use a generic one
end
[params, count] = sscanf(inputString,formatString);
if(count ~= numExpectedParams)
exception = MException(errorIdentifier,errorMessage);
throwAsCaller(exception);
end
end
end
end