[英]addOptional in Octave 5.1.0 is failing when parsing function input
这是我这里先前的一个问题的跟进。 尽管通过使用addParamValue和推荐功能已解决了上述问题中的紧迫问题,但使用addOptional仍然存在问题。 代码流为:
parser = inputParser() ;
parser.FunctionName = "mdDelay" ;
defaultMaxLag = 10 ;
checkMaxLag = @(x) validateattributes_with_return_value(x, {'numeric'}, {'positive', 'numel', 1}) ;
其中validateattributes_with_return_value是Octave的validateattributes的包装,因此返回true或false
function retval = validateattributes_with_return_value( varargin )
try
validateattributes( varargin{ : } ) ;
retval = true ;
catch
retval = false ;
end_try_catch
endfunction
然后使用
addRequired( parser , 'data' , @checkdata ) ;
addOptional( parser , 'maxLag' , defaultMaxLag , checkMaxLag ) ;
要么
addRequired( parser , 'data' , @checkdata ) ;
parser.addOptional( 'maxLag' , defaultMaxLag , checkMaxLag ) ;
其中checkdata是对输入数据是数字矢量或矩阵的简单检查
function check = checkdata( x )
check = false;
if (~isnumeric(x))
error('Input is not numeric');
elseif (numel(x) <= 1)
error('Input must be a vector or matrix');
else
check = true;
end
endfunction
其次是
parse( parser , data , varargin{:} ) ;
失败并显示错误消息
error: mdDelay: argument 'MAXLAG' is not a valid parameter
当这样称呼
tau = mdDelay( data , 'maxLag' , 25 ) ;
在这种情况下,数据只是一个2000行乘3列的数值矩阵。
我试图更改输入在代码中的显示顺序,以为“位置”可能是一个问题,但无济于事。
这不是主要的问题,因为我现在具有使用addParamValue的功能代码,但是也许这突出了Octave中的另一个已知错误?
您使用的addOptional
错误。 可选参数是可选参数,由其在参数列表中的位置标识。 因此,您应该这样称呼它:
mdDelay (data, 25); # 25 is the value for maxLag
由于将'maxLag'
(字符串)作为maxLag
(选项)的值传递'maxLag'
出现错误:
mdDelay (data, 'maxLag', 25); # 'maxLag' is the value for maxLag
当然, 'maxLag'
未通过测试:
checkMaxLag = @(x) validateattributes (x, {'numeric'}, {'positive', 'numel', 1}) ;
checkMaxLag('maxLag') # the string is not numeric
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.