简体   繁体   中英

input parser and function handle

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.

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 [] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM