问题描述
我编写了一个测试函数,并遇到了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 top.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 todefshape = '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:
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,
这篇关于inputParser验证参数未按预期运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!