简体   繁体   English

没有 varargin 的 inputParser

[英]inputParser without varargin

I would like to use inputParser on the function f(a,b,c,d) , where a is mandatory and bd are optional.我想在函数f(a,b,c,d)上使用 inputParser ,其中 a 是必需的, bd 是可选的。 I would like to keep bd named = not use f(a, varargin) , because that varargin doesn't tell you anything when you actually want to use the function.我想让 bd 命名 = 不使用f(a, varargin) ,因为当您真正想要使用该函数时,那个 varargin 不会告诉您任何事情。 The simple p.parse(a,b,c,d) obviously does not work unless all 4 parameters are given.除非给出所有 4 个参数,否则简单的p.parse(a,b,c,d)显然不起作用。

The only way I know how to do it is to use setDefaultValue(argname, value) , and then use p.parse(...) for checks if needed.我知道怎么做的唯一方法是使用setDefaultValue(argname, value) ,然后在需要时使用p.parse(...)进行检查。 But is there a simple way to hack this using just inputParser (without tons of ifs)?但是有没有一种简单的方法可以只使用 inputParser(没有大量的 ifs)来破解它?

As a side question, there is no easy way to do named arguments like in C# in MATLAB, right?作为一个附带问题,没有简单的方法可以像在 MATLAB 中的 C# 那样执行命名参数,对吗? So I would have that f(a,b,c,d) and use as such.所以我会有那个f(a,b,c,d)并使用它。 First argument is a, then b, then c ... But when the user wanted to call it like f(a, 'd', 3, 'c', 2) , he could.第一个参数是 a,然后是 b,然后是 c ...但是当用户想要像f(a, 'd', 3, 'c', 2)一样调用它时,他可以。 In this example, he would keep default b and use 3 for d and 2 for c.在这个例子中,他将保留默认值 b 并为 d 使用 3,为 c 使用 2。 Without tons of work on my side of course - I know how to do it, writing my own parsing of the thing, but I would like a simpler solution (like other person's parsing :))当然,我没有大量的工作 - 我知道该怎么做,编写我自己的解析,但我想要一个更简单的解决方案(就像其他人的解析 :))

Im going to give you an alternative answer: Don't be lazy, write docs.我会给你一个替代答案:不要偷懒,写文档。

If you do this:如果你这样做:

function [out]= foo(a,b,c,d)
%FOO does foo things
%
% FOO(a,b) does very foo thins to a and b
%
% FOO(a,b,c) is quite foo-y on c also
%
% FOO(a,b,c,d) c and d are optional FOO things

if (nargin==3)
...

then if the user presses F1 or types help foo they will get all the information in the comments.然后,如果用户按F1或键入help foo他们将获得评论中的所有信息。


To have users able to put inputs such as foo(1,2,'d',5) in my code, I use the following "inputparser" for optional parameters:为了让用户能够在我的代码中输入诸如foo(1,2,'d',5)的输入,我使用以下"inputparser"作为可选参数:

function [c,d]=parse_inputs(argin)
opts=     {'c','d'};
defaults=ones(length(opts),1);
% Check inputs
nVarargs = length(argin);
if mod(nVarargs,2)
    error('InvalidInput','Invalid number of inputs')
end

% check if option has been passed as input
for ii=1:2:nVarargs
    ind=find(ismember(opts,lower(argin{ii})));
    if ~isempty(ind)
        defaults(ind)=0;
    else
       error('InvalidInput',['Optional parameter "' argin{ii} '" does not exist' ]); 
    end
end

for ii=1:length(opts)
    opt=opts{ii};
    default=defaults(ii);
    % if one option is not default, then extrac value from input
    if default==0
        ind=double.empty(0,1);jj=1;
        while isempty(ind)
            ind=find(isequal(opt,lower(argin{jj})));
            jj=jj+1;
        end
         if isempty(ind)
            error('InvalidInput',['Optional parameter "' argin{jj} '" does not exist' ]); 
        end
        val=argin{jj};
    end

    switch opt
        case 'c': 
          if default;
             c=4;
          else
             c=val;
          end
     ...
     otherwise
            error('InvalidInput',['Invalid input name:', num2str(opt),'\n No such option in SART()']);
    end
end

Then The first thing I do inside foo is:然后我在 foo 里面做的第一件事是:

[c,d]=parse_inputs(varargin);

The first part of the question on how to handle varying number of inputs without varargin was answered well by Ander Biguri's answer . Ander Biguri 的回答很好地回答了关于如何在没有varargin情况下处理不同数量的输入的问题的第一部分。

Regarding the second part of the question on how to parse inputs in pairs, here is my simplified "input parser" :关于如何成对解析输入的问题的第二部分,这是我简化的“输入解析器”

function parseVarargin( argin )
% Simplified function to parse varargin property-value pairs

% Read input arguments
if mod(numel(argin),2)
    error('Uneven number of inputs')
end
for i = 1 : 2 : numel(argin)-1
    if ~ischar(argin{i})
        error('Properties must be strings')
    end
    isValid = evalin('caller', sprintf('exist(''%s'',''var'')', argin{i}));
    if isValid && ~strcmp(argin{i},'MRX')
        assignin('caller', argin{i}, argin{i+1})
    else
        error(['Undefined property %s. ', ...
            'Please note case-sensitivity.'], argin{i});
    end
end

To use it, simply set the default variable values and the call parseVarargin on varargin :要使用它,只需设置默认变量值并在parseVarargin上调用varargin

function foo( a, varargin )
%FOO general description
%
% Usage:
%   foo(a)
%   foo(a, 'property-value-pairs')
% 
% Properties:
%   b : This ...
%   c : That ...

% Default property values
b = 1;
c = [];

% Read property-value paris
parseVarargin( varargin );

% Do other stuff

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM