Permalink
Browse files

Added the ParseInput class to check for inputs.

  • Loading branch information...
1 parent 0c59a81 commit 4691758eb651b1b0a86e6dbcbaaf91ed526a45bb Laurent Hoeltgen committed Sep 30, 2012
@@ -0,0 +1,4 @@
+function obj = AddOptDoubleArray(obj, argName, default)
+obj.addOptional(argName, default, @IsDouble);
+obj.numOpt = obj.numOpt + 1;
+end
@@ -0,0 +1,4 @@
+function obj = AddParDoubleArray(obj, argName, default)
+obj.addParamValue(argName,default,@IsDouble);
+obj.numPar = obj.numPar + 1;
+end
@@ -0,0 +1,4 @@
+function obj = AddReqDoubleArray(obj, argName)
+obj.addRequired(argName,@IsDouble);
+obj.numReq = obj.numReq + 1;
+end
@@ -0,0 +1,3 @@
+function obj = AddReqDoubleVector(obj,argName)
+obj.addRequired(argName,@(x) IsDouble(x)&&isvector(x));
+end
@@ -0,0 +1,2 @@
+function obj = AddReqScalar(obj,argName)
+end
@@ -0,0 +1,5 @@
+function obj = CheckArguments(obj,numIn,numOut,varargin)
+obj.CheckNumInput(numIn);
+obj.CheckNumOutput(numOut);
+obj.parse(varargin{:});
+end
@@ -0,0 +1,8 @@
+function CheckNumInput(obj, numArgs)
+MExc = ExceptionMessage( ...
+ 'NumArg', ...
+ 'The number of input parameters does not match the number of parsed args.');
+assert( ...
+ numArgs <= obj.numReq + obj.numOpt + 2*obj.numPar, ...
+ MExc.id, MExc.message );
+end
@@ -0,0 +1,8 @@
+function CheckNumOutput(obj, numArgs)
+MExc = ExceptionMessage( ...
+ 'NumArg', ...
+ 'The number of input parameters does not match the number of parsed args.');
+assert( ...
+ (0 <= numArgs) && (numArgs <= obj.numOut), ...
+ MExc.id, MExc.message );
+end
View
@@ -0,0 +1,51 @@
+classdef ParseInput < inputParser
+
+ properties
+ numReq = 0;
+ numOpt = 0;
+ numPar = 0;
+ numOut = 1;
+ end
+
+ methods
+ function object = ParseInput(funName)
+
+ narginchk(0,1);
+ nargoutchk(0,1);
+
+ % Initialise super class.
+ object@inputParser();
+
+ % argument matches should be case sensitive.
+ object.CaseSensitive = true;
+
+ % keep unmatched parameters inside the object. This way we can pass
+ % options to methods defined inside methods by just passing the
+ % initial inputParser object.
+ object.KeepUnmatched = true;
+
+ % Whether to interpret a structure array as a single input or as a
+ % set of parameter name and value pairs.
+ object.StructExpand = true;
+
+ % the function name to be included in the error messages. If no
+ % argument is specified and if the object was created inside a
+ % function, that function name will be used. Otherwise it will be an
+ % empty string.
+ if nargin == 1
+ if IsString(funName)
+ object.FunctionName = funName;
+ else
+ MExc = ExceptionMessage( ...
+ 'BadArg', ...
+ 'If specified, argument must be 1d String' );
+ error(MExc.id, MExc.message);
+ end
+ end
+
+ end
+
+ end
+
+end
+

0 comments on commit 4691758

Please sign in to comment.