1
votes

I am using the Matlab input parser and want to parse a function handle using this code:

p = inputParser;
p.addOptional('progresscallback', 0, @(x) isa(x,'function_handle') );
p.parse(varargin{:});

This works well for a given function handle, but fails for no handle with

Argument 'progresscallback' failed validation @(x)isa(x,'function_handle').

Now I wonder how to contruct the testing function or the default value to make it work.

1

1 Answers

0
votes

If you just want to accept either empty or function handle inputs, you can modify the testing function like this:

@(x) isempty(x) || isa(x,'function_handle')

The short-circuit OR (||) won't evaluate the second half of the test if the first one is already true. BTW, you may also want to set your default value to [].