Octave 5.1.0 中的 addOptional 在解析函数输入时失败

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 的验证属性的包装器,因此返回 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 行 x 3 列的数值矩阵。

我曾尝试更改输入在代码中出现的顺序,认为这可能是 "positional," 的问题,但无济于事。

这不是主要问题,因为我现在有使用 addParamValue 的功能代码,但也许这突出了 Octave 中的另一个已知错误?

您使用的 addOptional 不正确。可选参数是一个可选参数,由其在参数列表中的位置标识。所以你应该这样称呼它:

mdDelay (data, 25); # 25 is the value for 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