inputParser验证参数未按预期运行 [英] inputParser validation argument not acting as expected

查看:244
本文介绍了inputParser验证参数未按预期运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个测试函数,并遇到了inputParser

I wrote a test function, and ran into trouble with inputParser

function doit(varargin)
p = inputParser;
p.KeepUnmatched = 1;
% why does this bomb if I put in @ischar ? 
p.addOptional('shape','forgot',@(x) true);
p.addParameter('color', 'k', @(x) true); % 'cause color could be char or triplet
p.parse(varargin{:});
%  more code using the parsed inputs
end

如果我跑步

doit('hello','color','g')

doit('color','g')

使用上面的代码,所有操作均按预期进行.但是,如果我将optional参数的输入验证更改为 p.addOptional('shape', 'forgot',@(x) ischar),我在p.parse(varargin{:})

with the code as above, all works as expected. But if I change the input validation for the optional argument to p.addOptional('shape', 'forgot',@(x) ischar) , I get an error at p.parse(varargin{:})

No value was given for 'g'. Name-value pair arguments require a name followed by a value.

似乎我不明白最终的参数是什么,应该用来验证输入参数的匿名函数.解决办法是什么?

It looks like I'm not understanding what the final argument, the anonymous function that's supposed to validate the input argument. What's the fix?

Win7上的Matlab 2015a.

Matlab 2015a on Win7 .

看来,varargin中的每个参数确切地影响什么类.

It appears that exactly what class each argument in varargin is affects the behavior.

function threeout = argthree( varargin)     
p = inputParser; 
p.KeepUnmatched = true; 
%defshape = 'forgot'; 
defshape = 5; 
addOptional(p,'shape',defshape,@isnumeric); 
addParameter(p,'color', 'k',@ischar); % 
parse(p, varargin{:}); 
threeout = p.Results; 
end

至少有两件事会导致parse()失败.首先:这:

There are at least two things that can cause parse() to fail. First: this :

>> argthree('nono','color','vu') 
Error using argthree (line 10) 
No value was given for 'vu'. Name-value pair arguments require a name followed by a value. 

请注意,即使应该对可选"形状进行数字测试,但解析器仍认为"nono"是名称-值对的开始.

Notice that even though the Optional "shape" is supposed to be tested for numeric, the parser appears to think "nono" is the start of a name-value pair.

Entering either a numer or nothng works: 
>> argthree('color','vu') 
ans = 
color: 'vu' 
shape: 5 
>> argthree(7,'color','vu') 
ans = 
color: 'vu' 
shape: 7

现在,不使用验证替换addOptional行: addOptional(p,'shape',defshape );

Now replace the addOptional line with no validation: addOptional(p,'shape',defshape );

对于所有三个呼叫,获得与上述相同的结果.

Get the same results as above for all three calls.

现在将默认值更改为 defshape = 'forgot' % a charstring

Now change the default to defshape = 'forgot' % a charstring

>> argthree('nono','color','vu') 
Error using argthree (line 10) 
No value was given for 'vu'. Name-value pair arguments require a name followed by a value. 
>> argthree('color','vu') 
ans = 
color: 'vu' 
shape: 'forgot' 
>> argthree(7,'color','vu') 
ans = 
color: 'vu' 
shape: 7 

现在尝试验证字符串输入:再次替换addOptional行- addOptional(p,'shape',defshape,@ischar );

Now try validating the string input: replace the addOptional line again- addOptional(p,'shape',defshape,@ischar );

>> argthree('nono','color','vu') 
ans = 
color: 'vu' 
shape: 'nono' 
>> argthree('color','vu') 
Error using argthree (line 10) 
No value was given for 'vu'. Name-value pair arguments require a name followed by a value. 
>> argthree(7,'color','vu') 
Error using argthree (line 10) 
The value of 'shape' is invalid. It must satisfy the function: ischar. 

这里与parse()尝试将varargin{:}匹配到参数列表(可选或名称/值对)的方式非常不一致.由于发生了不一致的故障,据我所知,我无法通过使用可选char参数后跟名称/值对来设置情况.

There's something very inconsistent here with how parse() tries to match varargin{:} to the parameter list (optional or name-value pair). Due to the inconsistent failures happening, I can't set up a situation with an Optional char argument followed by a name-value pair, so far as I can see.

我已经在MathWorks上提交了错误报告技术支持案例#02121531",他们能够重现该问题.

I've filed a bug report at MathWorks, "Technical Support Case #02121531" and they were able to reproduce the problem .

推荐答案

我一直在与MathWorks(技术支持案例#02121531)进行讨论.

I have been discussing this with MathWorks, Technical Support Case #02121531 .

我写了我想做的确切解释:

I wrote an explanation of exactly what I was hoping to do:

我希望能够有一个可选参数,即 字符串,然后是一个或多个值-参数"对 其中的参数是字符串.由于我将可选"解释为 就是这样,即varargin {:}元素之一 需要存在,我希望以下两个命令 会工作:

I would like to be able to have one Optional argument which is a character string, and then one or more Value-Parameter pairs, for which the parameters are strings. Since I interpreted "Optional" as being just that, i.e. one of the varargin{:} elements which does not need to be present, I expect that both of the following commands would work:

foop =parseCheck('nono','color','vu');

foop =parseCheck('color','vu');

但是, 解析器功能似乎无法理解'color'"是 值-参数对中的值.解析器看到一个字符串 作为第一个varargin,并立即将其标识为Optional 范围.我认为这既不直观也不理想.

However, the parser function appears unable to understand that " 'color' " is a Value in a Value-Parameter pair. The parser sees a character string as the first varargin and immediately identifies it as the Optional parameter. My view is that this is neither intuitive nor desirable.

我认为这是一个错误,建议将解析器代码设置为 重写,以便与任何值"字符串匹配的任何字符串都应为 首先进行测试以查看它是否导致有效的值-参数"对,以及 只有在那之后才应该测试初始varargin {:}元素 反对可选参数验证.

I think this is a bug, and would recommend that the parser code be rewritten so that any string which is a match to any "Value" string be first tested to see if it leads to a valid Value-Parameter pair, and only after that should the initial varargin{:} elements be tested against Optional argument validation.

基本上,我是说您不能将参数称为可选" 必须存在解析器才能正常运行.

Basically, I'm saying that you can't call an argument "Optional" if it has to be present for the parser to function properly.

我最后发现的是引用他们的支持者,

The last I have found out is, quoting their support person,

感谢您在我调查此问题期间的耐心配合.在 在您发送的示例中,颜色"确实已作为可选项使用 参数形状".之所以发生这种情况,是因为目前 参数优先于名称/值对.我很抱歉 给您带来不便.我已经分享了您的工作流程, 对开发人员的关注和建议以及增强功能 请求已提交.在将来的版本中将考虑使用它 MATLAB的.

Thank you for your patience while I was investigating the issue. In the example that you sent, ‘color’ is indeed consumed as the optional argument ‘Shape’. This is happening because currently, positional arguments take precedence over name/value pairs. I do apologize for the inconvenience this may cause you. I have shared your workflow, concerns and your suggestions with the developers and an enhancement request has been submitted. It is to be considered in future releases of MATLAB.

这篇关于inputParser验证参数未按预期运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆