Matlab validating inputs with input parser


08-Aug-2016 09:49

function [ v ] = Alg( b,shape,varargin ) %%Parse inputs p = input Parser; add Required(p,'b',@isnumeric); expected Shapes = ; add Required(p,'shape',@(x) any(validatestring(x,expected Shapes))); default It = 42; add Parameter(p,'It',default Iter,@isnumeric); add Parameter(p,'t',@isnumeric); add Parameter(p,'v',@isnumeric); parse(p,b,shape,varargin) b = p.

Results.v; parse(p,b,shape,varargin) default Point = Alg_sq(b, Pt, It); default Point = Sub_Alg(default Point,shape,t,v); add Parameter(p,'Pt',default Point,@isnumeric); Pt = p. Pt; %%Shape switch shape case 'circle' v = Alg_crcl( b, Pt, It ); case 'square' v = Alg_sq( b, Pt, It ); end end function v = Alg(b, shape, varargin) % define arguments p = input Parser; add Required(p, 'b', @isnumeric); add Required(p, 'shape', @(x) any(validatestring(x,))); add Parameter(p, 'It', 42, @isnumeric); add Parameter(p, 't', @isnumeric); add Parameter(p, 'v', @isnumeric); add Parameter(p, 'Pt', [], @isnumeric); % default for now is empty matrix % parse arguments parse(p, b, shape, varargin) b = p.

Summary of important user-visible changes for general 2.0.0: ------------------------------------------------------------------- ** Package is now dependent on Octave version 4.0.0 to avoid conflicts with missing input Parser.

** The @input Parser has been removed since it is now part of Octave core and has a completely Matlab compatible syntax.

A rather elegant way is described in this answer on SO.

The answer is indeed (as usual for SO) rather compact...

Only if you have an old version should you need to fall back on syntaxes from other answers that use coming from? Because that effects the validity of this solution After a few seconds of googling I found that as is often the case, Loren Shure has already solved this problem for us.Since the argument names are local to the function, Matlab has no way of telling whether the second or the third optional argument is missing.



Sexcams is a live cam service where users can watch free sex cams, participate in sex chat, and broadcast their own live sex cam – all for free!… continue reading »


Read more

On Celebrity Apprentice, she quickly became embroiled in a personal feud with Piers Morgan.… continue reading »


Read more