Reputation: 552
This is a follow up to an earlier question of mine here. Although the immediate problem in said question has been solved by use of addParamValue and the recommendated function, there is still an issue with using addOptional. The code flow is:
parser = inputParser() ;
parser.FunctionName = "mdDelay" ;
defaultMaxLag = 10 ;
checkMaxLag = @(x) validateattributes_with_return_value(x, {'numeric'}, {'positive', 'numel', 1}) ;
where validateattributes_with_return_value is a wrapper around Octave's validateattributes so that true or false is returned
function retval = validateattributes_with_return_value( varargin )
try
validateattributes( varargin{ : } ) ;
retval = true ;
catch
retval = false ;
end_try_catch
endfunction
then using either of
addRequired( parser , 'data' , @checkdata ) ;
addOptional( parser , 'maxLag' , defaultMaxLag , checkMaxLag ) ;
or
addRequired( parser , 'data' , @checkdata ) ;
parser.addOptional( 'maxLag' , defaultMaxLag , checkMaxLag ) ;
where checkdata is a simple check that the input data is a numeric vector or matrix
function check = checkdata( x )
check = false;
if (~isnumeric(x))
error('Input is not numeric');
elseif (numel(x) <= 1)
error('Input must be a vector or matrix');
else
check = true;
end
endfunction
followed by
parse( parser , data , varargin{:} ) ;
fails with the error message
error: mdDelay: argument 'MAXLAG' is not a valid parameter
when called thus
tau = mdDelay( data , 'maxLag' , 25 ) ;
In this case the data is simply a 2000 row by 3 column matrix of numeric values.
I have tried changing the order the inputs appear in the code, thinking that it could be a problem with being "positional," but to no avail.
This is not a major concern as I now have functioning code using addParamValue, but maybe this highlights another known bug in Octave?
Upvotes: 0
Views: 183
Reputation: 13091
You are using addOptional
incorrectly. An optional parameter is an optional argument that is identified by its position on the list of arguments. So you should be calling it like so:
mdDelay (data, 25); # 25 is the value for maxLag
You are getting an error because your passing 'maxLag'
(the string) as the value for maxLag
(the option):
mdDelay (data, 'maxLag', 25); # 'maxLag' is the value for maxLag
And of course 'maxLag'
fails the test:
checkMaxLag = @(x) validateattributes (x, {'numeric'}, {'positive', 'numel', 1}) ;
checkMaxLag('maxLag') # the string is not numeric
Upvotes: 1