如何在MATLAB中处理函数参数的名称/值对

时间:2010-05-05 17:03:07

标签: function matlab arguments function-parameter name-value

我有一个将可选参数作为名称/值对的函数。

function example(varargin)
% Lots of set up stuff
vargs = varargin;
nargs = length(vargs);
names = vargs(1:2:nargs);
values = vargs(2:2:nargs);

validnames = {'foo', 'bar', 'baz'};    
for name = names
   validatestring(name{:}, validnames);
end

% Do something ...
foo = strmatch('foo', names);
disp(values(foo))
end

example('foo', 1:10, 'bar', 'qwerty')

似乎在提取适当的值时需要付出很多努力(并且它仍然不是特别强大的再次严格指定的输入)。有没有更好的方法来处理这些名称/值对?是否有MATLAB附带的辅助函数来协助?

14 个答案:

答案 0 :(得分:56)

我更喜欢使用结构作为我的选择。这为您提供了一种存储选项的简便方法,以及一种简单的方法来定义它们。而且,整个事情变得相当紧凑。

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

答案 1 :(得分:43)

InputParser对此有所帮助。有关详细信息,请参阅Parse Function Inputs

答案 2 :(得分:11)

我可能会花费几个小时来讨论这个问题,但仍然没有一般Matlab签名处理的完美格式。但这里有几条建议。

首先,采用自由放任的方法来验证输入类型。相信来电者。如果你真的想要强类型测试,你需要像Java这样的静态语言。尝试在Matlab中的每个位置强制执行类型安全,并且最终会有大部分LOC和执行时间专门用于运行时类型测试和用户域中的强制,这会在Matlab的大量功能和开发速度中进行交易。我很难学到这一点。

对于API签名(旨在从其他函数调用的函数,而不是从命令行调用的函数),请考虑使用单个Args参数而不是varargin。然后它可以在多个参数之间传递,而不必将其转换为逗号分隔的varargin签名列表。像Jonas说的那样,结构非常方便。结构体和n-by-2 {name,value; ...}单元格之间也有一个很好的同构,你可以设置一些函数在函数内部将它们转换为内部使用的任何函数。

function example(args)
%EXAMPLE
%
% Where args is a struct or {name,val;...} cell array

无论您是使用inputParser还是像其他精美示例一样滚动自己的名称/ val解析器,请将其打包在一个单独的标准函数中,您可以从具有name / val签名的函数顶部调用它。让它接受一个方便写出的数据结构中的默认值列表,并且你的arg解析调用看起来有点像函数签名声明,这有助于提高可读性,并避免复制和粘贴样板代码。

这是解析调用的样子。

function out = my_example_function(varargin)
%MY_EXAMPLE_FUNCTION Example function 

% No type handling
args = parsemyargs(varargin, {
    'Stations'  {'ORD','SFO','LGA'}
    'Reading'   'Min Temp'
    'FromDate'  '1/1/2000'
    'ToDate'    today
    'Units'     'deg. C'
    });
fprintf('\nArgs:\n');
disp(args);

% With type handling
typed_args = parsemyargs(varargin, {
    'Stations'  {'ORD','SFO','LGA'}     'cellstr'
    'Reading'   'Min Temp'              []
    'FromDate'  '1/1/2000'              'datenum'
    'ToDate'    today                   'datenum'
    'Units'     'deg. C'                []
    });
fprintf('\nWith type handling:\n');
disp(typed_args);

% And now in your function body, you just reference stuff like
% args.Stations
% args.FromDate

这是一个以这种方式实现名称/ val解析的函数。您可以将其挖空并用inputParser替换它,您自己的类型约定等。我认为n-by-2单元格约定使得可读性很好的源代码;考虑保持这一点。在接收代码中,结构通常更方便处理,但是使用表达式和文字构造n-by-2单元更方便。 (结构需要每行的“,......”延续,并保护单元格值从扩展到非标量结构。)

function out = parsemyargs(args, defaults)
%PARSEMYARGS Arg parser helper
%
% out = parsemyargs(Args, Defaults)
%
% Parses name/value argument pairs.
%
% Args is what you pass your varargin in to. It may be
%
% ArgTypes is a list of argument names, default values, and optionally
% argument types for the inputs. It is an n-by-1, n-by-2 or n-by-3 cell in one
% of these forms forms:
%   { Name; ... }
%   { Name, DefaultValue; ... }
%   { Name, DefaultValue, Type; ... }
% You may also pass a struct, which is converted to the first form, or a
% cell row vector containing name/value pairs as 
%   { Name,DefaultValue, Name,DefaultValue,... }
% Row vectors are only supported because it's unambiguous when the 2-d form
% has at most 3 columns. If there were more columns possible, I think you'd
% have to require the 2-d form because 4-element long vectors would be
% ambiguous as to whether they were on record, or two records with two
% columns omitted.
%
% Returns struct.
%
% This is slow - don't use name/value signatures functions that will called
% in tight loops.

args = structify(args);
defaults = parse_defaults(defaults);

% You could normalize case if you want to. I recommend you don't; it's a runtime cost
% and just one more potential source of inconsistency.
%[args,defaults] = normalize_case_somehow(args, defaults);

out = merge_args(args, defaults);

%%
function out = parse_defaults(x)
%PARSE_DEFAULTS Parse the default arg spec structure
%
% Returns n-by-3 cellrec in form {Name,DefaultValue,Type;...}.

if isstruct(x)
    if ~isscalar(x)
        error('struct defaults must be scalar');
    end
    x = [fieldnames(s) struct2cell(s)];
end
if ~iscell(x)
    error('invalid defaults');
end

% Allow {name,val, name,val,...} row vectors
% Does not work for the general case of >3 columns in the 2-d form!
if size(x,1) == 1 && size(x,2) > 3
    x = reshape(x, [numel(x)/2 2]);
end

% Fill in omitted columns
if size(x,2) < 2
    x(:,2) = {[]}; % Make everything default to value []
end
if size(x,2) < 3
    x(:,3) = {[]}; % No default type conversion
end

out = x;

%%
function out = structify(x)
%STRUCTIFY Convert a struct or name/value list or record list to struct

if isempty(x)
    out = struct;
elseif iscell(x)
    % Cells can be {name,val;...} or {name,val,...}
    if (size(x,1) == 1) && size(x,2) > 2
        % Reshape {name,val, name,val, ... } list to {name,val; ... }
        x = reshape(x, [2 numel(x)/2]);
    end
    if size(x,2) ~= 2
        error('Invalid args: cells must be n-by-2 {name,val;...} or vector {name,val,...} list');
    end

    % Convert {name,val, name,val, ...} list to struct
    if ~iscellstr(x(:,1))
        error('Invalid names in name/val argument list');
    end
    % Little trick for building structs from name/vals
    % This protects cellstr arguments from expanding into nonscalar structs
    x(:,2) = num2cell(x(:,2)); 
    x = x';
    x = x(:);
    out = struct(x{:});
elseif isstruct(x)
    if ~isscalar(x)
        error('struct args must be scalar');
    end
    out = x;
end

%%
function out = merge_args(args, defaults)

out = structify(defaults(:,[1 2]));
% Apply user arguments
% You could normalize case if you wanted, but I avoid it because it's a
% runtime cost and one more chance for inconsistency.
names = fieldnames(args);
for i = 1:numel(names)
    out.(names{i}) = args.(names{i});
end
% Check and convert types
for i = 1:size(defaults,1)
    [name,defaultVal,type] = defaults{i,:};
    if ~isempty(type)
        out.(name) = needa(type, out.(name), type);
    end
end

%%
function out = needa(type, value, name)
%NEEDA Check that a value is of a given type, and convert if needed
%
% out = needa(type, value)

% HACK to support common 'pseudotypes' that aren't real Matlab types
switch type
    case 'cellstr'
        isThatType = iscellstr(value);
    case 'datenum'
        isThatType = isnumeric(value);
    otherwise
        isThatType = isa(value, type);
end

if isThatType
    out = value;
else
    % Here you can auto-convert if you're feeling brave. Assumes that the
    % conversion constructor form of all type names works.
    % Unfortunately this ends up with bad results if you try converting
    % between string and number (you get Unicode encoding/decoding). Use
    % at your discretion.
    % If you don't want to try autoconverting, just throw an error instead,
    % with:
    % error('Argument %s must be a %s; got a %s', name, type, class(value));
    try
        out = feval(type, value);
    catch err
        error('Failed converting argument %s from %s to %s: %s',...
            name, class(value), type, err.message);
    end
end

非常不幸的是,字符串和日期不是Matlab中的第一类。

答案 3 :(得分:6)

我个人使用从许多统计工具箱函数使用的私有方法派生的自定义函数(如kmeans,pca,svmtrain,ttest2,...)

作为内部实用程序功能,它已更改并在发布时重命名了多次。根据您的MATLAB版本,尝试查找以下文件之一:

%# old versions
which -all statgetargs
which -all internal.stats.getargs
which -all internal.stats.parseArgs

%# current one, as of R2014a
which -all statslib.internal.parseArgs

与任何未记录的函数一样,没有任何保证,可以在后续版本中从MATLAB中删除而不另行通知......无论如何,我相信有人在文件交换中将getargs的旧版本发布为parsing functions inputs ..

该函数使用一组有效参数名称及其默认值将参数作为名称/值对处理。它将解析的参数作为单独的输出变量返回。默认情况下,无法识别的名称/值对会引发错误,但我们也可以在额外的输出中静默捕获它们。这是功能描述:

$MATLABROOT\toolbox\stats\stats\+internal\+stats\parseArgs.m

function varargout = parseArgs(pnames, dflts, varargin)
%
% [A,B,...] = parseArgs(PNAMES, DFLTS, 'NAME1',VAL1, 'NAME2',VAL2, ...)
%   PNAMES   : cell array of N valid parameter names.
%   DFLTS    : cell array of N default values for these parameters.
%   varargin : Remaining arguments as name/value pairs to be parsed.
%   [A,B,...]: N outputs assigned in the same order as the names in PNAMES.
%
% [A,B,...,SETFLAG] = parseArgs(...)
%   SETFLAG  : structure of N fields for each parameter, indicates whether
%              the value was parsed from input, or taken from the defaults.
%
% [A,B,...,SETFLAG,EXTRA] = parseArgs(...)
%   EXTRA    : cell array containing name/value parameters pairs not
%              specified in PNAMES.

实施例

function my_plot(x, varargin)
    %# valid parameters, and their default values
    pnames = {'Color', 'LineWidth', 'LineStyle', 'Title'};
    dflts  = {    'r',           2,        '--',      []};

    %# parse function arguments
    [clr,lw,ls,txt] = internal.stats.parseArgs(pnames, dflts, varargin{:});

    %# use the processed values: clr, lw, ls, txt
    %# corresponding to the specified parameters
    %# ...
end

现在可以通过以下任何方式调用此示例函数:

>> my_plot(data)                                %# use the defaults
>> my_plot(data, 'linestyle','-', 'Color','b')  %# any order, case insensitive
>> my_plot(data, 'Col',[0.5 0.5 0.5])           %# partial name match

以下是一些无效的调用和抛出的错误:

%# unrecognized parameter
>> my_plot(x, 'width',0)
Error using [...]
Invalid parameter name: width.

%# bad parameter
>> my_plot(x, 1,2)
Error using [...]
Parameter name must be text.

%# wrong number of arguments
>> my_plot(x, 'invalid')
Error using [...]
Wrong number of arguments.

%# ambiguous partial match
>> my_plot(x, 'line','-')
Error using [...]
Ambiguous parameter name: line.

inputParser:

正如其他人所提到的,官方推荐的inputParser方法是使用validation类。它支持各种方案,例如指定所需的输入,可选的位置参数和名称/值参数。它还允许对输入执行{{3}}(例如检查类/类型和参数的大小/形状)

答案 4 :(得分:5)

在此问题上阅读Loren's informative post。不要忘记阅读评论部分...... - 您将看到有很多不同的方法来解决这个问题。它们都有效,所以选择一种优先方法实际上是个人品味和可维护性的问题。

答案 5 :(得分:4)

MathWorks恢复了这匹老马,但具有非常有用的功能,可以直接满足这一需求。它称为 Function Argument Validation (功能参数验证)(一个短语,可以并且应该在文档中搜索),并且随发行版R2019b +一起提供。 MathWorks也创建了有关它的视频。验证工作就像人们多年来提出的“技巧”一样。这是一个示例:

function ret = example( inputDir, proj, options )
%EXAMPLE An example.
% Do it like this.
% See THEOTHEREXAMPLE.

    arguments
        inputDir (1, :) char
        proj (1, 1) projector
        options.foo char {mustBeMember(options.foo, {'bar' 'baz'})} = 'bar'
        options.Angle (1, 1) {double, integer} = 45
        options.Plot (1, 1) logical = false
    end

    % Code always follows 'arguments' block.
    ret = [];
    switch options.foo
        case 'bar'
            ret = sind(options.Angle);
        case 'baz'
            ret = cosd(options.Angle);
    end
end

这里是开箱包:

arguments块必须位于任何代码之前(帮助块之后为OK),并且必须遵循函数定义中定义的位置顺序,我相信每个参数都需要提及。必需的参数排在第一位,其次是可选参数,然后是名称-值对。 MathWorks还建议不要再使用varargin关键字,但是narginnargout仍然有用。

  • 在这种情况下,类要求可以是自定义类,例如projector
  • 必需的参数可能没有默认值。是众所周知的,因为它们没有默认值。
  • 可选参数必须具有默认值;是众所周知的,因为它们具有默认值。
  • 默认值必须能够通过相同的参数验证。换句话说,默认值zeros(3)不能用作假定是字符向量的参数的默认值。
  • 名称/值对存储在options结构中(提示我们可以使用该结构传递关键字参数,例如Python中的kwargs)。
  • 很好,当您在函数调用中按Tab键时,名称-值参数现在将显示为参数提示。

因此,在此示例中,inputDir是必需的参数,因为没有给出默认值。它还必须是1xN的字符向量。似乎与该语句矛盾,请注意,MATLAB将尝试转换提供的参数,以查看转换后的参数是否通过。例如,如果您将12传递为inputDir,它将通过inputDir == '12'。相反,zeros(3)不起作用,但是由于它是一个数组。当指定字符时,别忘了使字符串失败,反之亦然。 MATLAB知道您想要什么并进行转换。您需要更深入地研究才能避免这种“灵活性”。

'foo'指定一个名称/值对,其值只能是'bar''baz'

MATLAB具有许多mustBe...验证功能(开始输入 mustBe,然后点击选项卡以查看可用的内容),它非常容易 创建自己的。如果创建自己的验证功能,则必须 如果输入不匹配,则提供错误,例如uigetdir, 如果用户取消对话框,则返回0。就我个人而言 遵循MATLAB的约定并调用我的验证函数 mustBe...,因此我拥有类似mustBeNatural的功能 数字和mustBeFile来确保我传递了一个实际上 存在。

'Angle'指定了一个名称-值对,其值必须是标量双精度或整数,因此,例如example(pwd, 'foo', 'baz', 'Angle' [30 70])自传递矢量以来将不起作用。

您明白了。 arguments块具有很大的灵活性-我认为太多或太多-但对于简单功能而言,它既快速又容易。您仍然可以依靠inputParservalidateattributesassert等中的一个或多个来解决更大的验证复杂性,但是我总是尝试将内容塞入arguments首先,封锁。如果变得难看,我将转到其他选项。

答案 6 :(得分:3)

我喜欢这样的本土锅炉板代码:

function TestExample(req1, req2, varargin)
for i = 1:2:length(varargin)
    if strcmpi(varargin{i}, 'alphabet')
        ALPHA = varargin{i+1};

    elseif strcmpi(varargin{i}, 'cutoff')
        CUTOFF = varargin{i+1};
        %we need to remove these so seqlogo doesn't get confused
        rm_inds = [rm_inds i, i+1]; %#ok<*AGROW>

    elseif strcmpi(varargin{i}, 'colors')
        colors = varargin{i+1};
        rm_inds = [rm_inds i, i+1]; 
    elseif strcmpi(varargin{i}, 'axes_handle')
        handle = varargin{i+1};
        rm_inds = [rm_inds i, i+1]; 
    elseif strcmpi(varargin{i}, 'top-n')
        TOPN = varargin{i+1};
        rm_inds = [rm_inds i, i+1];
    elseif strcmpi(varargin{i}, 'inds')
        npos = varargin{i+1};
        rm_inds = [rm_inds i, i+1];
    elseif strcmpi(varargin{i}, 'letterfile')
        LETTERFILE = varargin{i+1};
        rm_inds = [rm_inds i, i+1];
    elseif strcmpi(varargin{i}, 'letterstruct')
        lo = varargin{i+1};
        rm_inds = [rm_inds i, i+1];
    end
end

这样我可以模拟'选项',这个值对几乎与大多数Matlab函数的参数相同。

希望有所帮助,

威尔

答案 7 :(得分:1)

根据Jonas的想法,这是我正在试用的解决方案。

function argStruct = NameValuePairToStruct(defaults, varargin)
%NAMEVALUEPAIRTOSTRUCT Converts name/value pairs to a struct.
% 
% ARGSTRUCT = NAMEVALUEPAIRTOSTRUCT(DEFAULTS, VARARGIN) converts
% name/value pairs to a struct, with defaults.  The function expects an
% even number of arguments to VARARGIN, alternating NAME then VALUE.
% (Each NAME should be a valid variable name.)
% 
% Examples: 
% 
% No defaults
% NameValuePairToStruct(struct, ...
%    'foo', 123, ...
%    'bar', 'qwerty', ...
%    'baz', magic(3))
% 
% With defaults
% NameValuePairToStruct( ...
%    struct('bar', 'dvorak', 'quux', eye(3)), ...
%    'foo', 123, ...
%    'bar', 'qwerty', ...
%    'baz', magic(3))
% 
% See also: inputParser

nArgs = length(varargin);
if rem(nArgs, 2) ~= 0
   error('NameValuePairToStruct:NotNameValuePairs', ...
      'Inputs were not name/value pairs');
end

argStruct = defaults;
for i = 1:2:nArgs
   name = varargin{i};
   if ~isvarname(name)
      error('NameValuePairToStruct:InvalidName', ...
         'A variable name was not valid');
   end
   argStruct = setfield(argStruct, name, varargin{i + 1});  %#ok<SFLD>
end

end

答案 8 :(得分:1)

灵感来自乔纳斯的答案,但更紧凑:

function example(varargin)
  defaults = struct('A',1, 'B',magic(3));  %define default values

  params = struct(varargin{:});
  for f = fieldnames(defaults)',
    if ~isfield(params, f{1}),
      params.(f{1}) = defaults.(f{1});
    end
  end

  %now just access them as params.A, params.B

答案 9 :(得分:1)

自从我使用process_options.m以来。它稳定,易于使用,并已包含在各种matlab框架中。虽然对性能一无所知 - 可能会有更快的实现。

我最喜欢process_options的功能是unused_args返回值,可用于在args组中拆分输入参数,例如子进程。

您可以轻松定义默认值。

最重要的是:使用process_options.m通常会产生可读可维护选项定义。

示例代码:

function y = func(x, y, varargin)

    [u, v] = process_options(varargin,
                             'u', 0,
                             'v', 1);

答案 10 :(得分:1)

如果您可以访问MATLAB的财务工具箱,那么有一个名为parsepvpairs的漂亮功能可以很好地处理这个问题。它需要三个参数,预期字段名称,默认字段值和接收的实际参数。

例如,这是一个在MATLAB中创建HTML图形的函数,可以使用名为&#39; url&#39;,&#39; html&#39;和&#39的可选字段值对;标题&#39 ;.

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

答案 11 :(得分:0)

function argtest(varargin)

a = 1;

for ii=1:length(varargin)/2
    [~] = evalc([varargin{2*ii-1} '=''' num2str(varargin{2*ii}) '''']);
end;

disp(a);
who

这当然不会检查正确的分配,但它很简单,无论如何都会忽略任何无用的变量。它也只适用于数字,字符串和数组,但不适用于矩阵,单元格或结构。

答案 12 :(得分:0)

我今天最终写了这篇文章,然后发现了这些提及。 我使用struct和struct'overlays'作为选项。它基本上反映了setstructfields()的功能,只是无法添加新参数。它还有一个递归选项,而setstructfields()则自动执行。 它可以通过调用struct(args {:})来获取配对值的单元格数组。

% Overlay default fields with input fields
% Good for option management
% Arguments
%   $opts - Default options
%   $optsIn - Input options
%       Can be struct(), cell of {name, value, ...}, or empty []
%   $recurseStructs - Applies optOverlay to any existing structs, given new
%   value is a struct too and both are 1x1 structs
% Output
%   $opts - Outputs with optsIn values overlayed
function [opts] = optOverlay(opts, optsIn, recurseStructs)
    if nargin < 3
        recurseStructs = false;
    end
    isValid = @(o) isstruct(o) && length(o) == 1;
    assert(isValid(opts), 'Existing options cannot be cell array');
    assert(isValid(optsIn), 'Input options cannot be cell array');
    if ~isempty(optsIn)
        if iscell(optsIn)
            optsIn = struct(optsIn{:});
        end
        assert(isstruct(optsIn));
        fields = fieldnames(optsIn);
        for i = 1:length(fields)
            field = fields{i};
            assert(isfield(opts, field), 'Field does not exist: %s', field);
            newValue = optsIn.(field);
            % Apply recursion
            if recurseStructs
                curValue = opts.(field);
                % Both values must be proper option structs
                if isValid(curValue) && isValid(newValue) 
                    newValue = optOverlay(curValue, newValue, true);
                end
            end
            opts.(field) = newValue;
        end
    end
end

我会说使用命名约定'defaults'和'new'可能会更好:P

答案 13 :(得分:0)

我根据Jonas和Richie Cotton制作了一个功能。它实现了两个功能(灵活的参数或限制,意味着只允许存在于默认值中的变量),以及一些其他的东西,如语法糖和健全性检查。

function argStruct = getnargs(varargin, defaults, restrict_flag)
%GETNARGS Converts name/value pairs to a struct (this allows to process named optional arguments).
% 
% ARGSTRUCT = GETNARGS(VARARGIN, DEFAULTS, restrict_flag) converts
% name/value pairs to a struct, with defaults.  The function expects an
% even number of arguments in VARARGIN, alternating NAME then VALUE.
% (Each NAME should be a valid variable name and is case sensitive.)
% Also VARARGIN should be a cell, and defaults should be a struct().
% Optionally: you can set restrict_flag to true if you want that only arguments names specified in defaults be allowed. Also, if restrict_flag = 2, arguments that aren't in the defaults will just be ignored.
% After calling this function, you can access your arguments using: argstruct.your_argument_name
%
% Examples: 
%
% No defaults
% getnargs( {'foo', 123, 'bar', 'qwerty'} )
%
% With defaults
% getnargs( {'foo', 123, 'bar', 'qwerty'} , ...
%               struct('foo', 987, 'bar', magic(3)) )
%
% See also: inputParser
%
% Authors: Jonas, Richie Cotton and LRQ3000
%

    % Extract the arguments if it's inside a sub-struct (happens on Octave), because anyway it's impossible that the number of argument be 1 (you need at least a couple, thus two)
    if (numel(varargin) == 1)
        varargin = varargin{:};
    end

    % Sanity check: we need a multiple of couples, if we get an odd number of arguments then that's wrong (probably missing a value somewhere)
    nArgs = length(varargin);
    if rem(nArgs, 2) ~= 0
        error('NameValuePairToStruct:NotNameValuePairs', ...
            'Inputs were not name/value pairs');
    end

    % Sanity check: if defaults is not supplied, it's by default an empty struct
    if ~exist('defaults', 'var')
        defaults = struct;
    end
    if ~exist('restrict_flag', 'var')
        restrict_flag = false;
    end

    % Syntactic sugar: if defaults is also a cell instead of a struct, we convert it on-the-fly
    if iscell(defaults)
        defaults = struct(defaults{:});
    end

    optionNames = fieldnames(defaults); % extract all default arguments names (useful for restrict_flag)

    argStruct = defaults; % copy over the defaults: by default, all arguments will have the default value.After we will simply overwrite the defaults with the user specified values.
    for i = 1:2:nArgs % iterate over couples of argument/value
        varname = varargin{i}; % make case insensitive
        % check that the supplied name is a valid variable identifier (it does not check if the variable is allowed/declared in defaults, just that it's a possible variable name!)
        if ~isvarname(varname)
          error('NameValuePairToStruct:InvalidName', ...
             'A variable name was not valid: %s position %i', varname, i);
        % if options are restricted, check that the argument's name exists in the supplied defaults, else we throw an error. With this we can allow only a restricted range of arguments by specifying in the defaults.
        elseif restrict_flag && ~isempty(defaults) && ~any(strmatch(varname, optionNames))
            if restrict_flag ~= 2 % restrict_flag = 2 means that we just ignore this argument, else we show an error
                error('%s is not a recognized argument name', varname);
            end
        % else alright, we replace the default value for this argument with the user supplied one (or we create the variable if it wasn't in the defaults and there's no restrict_flag)
        else
            argStruct = setfield(argStruct, varname, varargin{i + 1});  %#ok<SFLD>
        end
    end

end

available as a Gist

对于那些有兴趣拥有真实命名参数的人(使用类似于Python的语法,例如:myfunction(a = 1,b =&#39; qwerty&#39;)),使用InputParser(仅适用于Matlab,Octave用户将必须至少等到v4.2或者你可以尝试一个名为InputParser2的包装器。

另外作为奖励,如果您不想总是输入argstruct.yourvar但直接使用yourvar,则可以使用以下snippet by Jason S

function varspull(s)
% Import variables in a structures into the local namespace/workspace
% eg: s = struct('foo', 1, 'bar', 'qwerty'); varspull(s); disp(foo); disp(bar);
% Will print: 1 and qwerty
% 
%
% Author: Jason S
%
    for n = fieldnames(s)'
        name = n{1};
        value = s.(name);
        assignin('caller',name,value);
    end
end