How to deal with name/value pairs of function arguments in MATLAB

后端 未结 14 1100
忘掉有多难
忘掉有多难 2020-11-28 19:28

I have a function that takes optional arguments as name/value pairs.

function example(varargin)
% Lots of set up stuff
vargs = varargin;
nargs = length(vargs         


        
14条回答
  •  伪装坚强ぢ
    2020-11-28 19:42

    I prefer using structures for my options. This gives you an easy way to store the options and an easy way to define them. Also, the whole thing becomes rather compact.

    function example(varargin)
    
    %# define defaults at the beginning of the code so that you do not need to
    %# scroll way down in case you want to change something or if the help is
    %# incomplete
    options = struct('firstparameter',1,'secondparameter',magic(3));
    
    %# read the acceptable names
    optionNames = fieldnames(options);
    
    %# count arguments
    nArgs = length(varargin);
    if round(nArgs/2)~=nArgs/2
       error('EXAMPLE needs propertyName/propertyValue pairs')
    end
    
    for pair = reshape(varargin,2,[]) %# pair is {propName;propValue}
       inpName = lower(pair{1}); %# make case insensitive
    
       if any(strcmp(inpName,optionNames))
          %# overwrite options. If you want you can test for the right class here
          %# Also, if you find out that there is an option you keep getting wrong,
          %# you can use "if strcmp(inpName,'problemOption'),testMore,end"-statements
          options.(inpName) = pair{2};
       else
          error('%s is not a recognized parameter name',inpName)
       end
    end
    

提交回复
热议问题