matlab inputparser

时间:2013-02-12 18:59:19

标签: matlab

我想用inputparser验证输入参数,其中输入参数有2 required inputsvarargin

  function = myfun( input1,input2,varargin)
  p = inputParser;
  addRequired(p,'input1',@(x) any(validatestring(x,{'sum','sub'})))
  addRequired(p,'input2',@ischar)

  parse(p,input1,input2,varargin{:})  ??

如何验证varargin我的varargin的长度是多少。

喜欢:myfun('sum','hallo','Name','address',...)

1 个答案:

答案 0 :(得分:0)

如果(且仅当)您的变量输入参数采用参数 - 值对的形式,您可以这样做:

function result = myfun(varargin)
    p = inputParser;
    p.addRequired('input1', @validation1);
    p.addRquuired('input2', @validation2);
    p.KeepUnmatched = true;
    p.parse(varargin{:});
    params = p.Unmatched;
end

否则,你很难手动解析输入,我担心:

function result = myfun(input1, input2, varargin)
    p = inputParser;
    p.addRequired('input1', @validation1);
    p.addRquuired('input2', @validation2);
    p.parse(input1, input2);

    for item in varargin:
        % MANUAL VALIDATION GOES HERE
    end

end