简体   繁体   English

输入解析器和函数句柄

[英]input parser and function handle

I am using the Matlab input parser and want to parse a function handle using this code: 我正在使用Matlab输入解析器,并希望使用以下代码来解析函数句柄:

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'). 参数'progresscallback'验证@(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. 如果第一个已经正确,则短路OR( || )将不会评估测试的第二个一半。 BTW, you may also want to set your default value to [] . 顺便说一句,您可能还需要将默认值设置为[]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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