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

后端 未结 14 1097
忘掉有多难
忘掉有多难 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:45

    There is a nifty function called parsepvpairs that takes care of this nicely, provided you have access to MATLAB's finance toolbox. It takes three arguments, expected field names, default field values, and the actual arguments received.

    For example, here's a function that creates an HTML figure in MATLAB and can take the optional field value pairs named 'url', 'html', and 'title'.

    function htmldlg(varargin)
        names = {'url','html','title'};
        defaults = {[],[],'Padaco Help'};
        [url, html,titleStr] = parsepvpairs(names,defaults,varargin{:});
    
        %... code to create figure using the parsed input values
    end
    

提交回复
热议问题