Home > General-Functions > parseArguments.m

parseArguments

PURPOSE ^

PARSEARGUMENTS parses a list of pairs {ArgName, ArgValue}

SYNOPSIS ^

function args = parseArguments(args, varargin)

DESCRIPTION ^

 PARSEARGUMENTS parses a list of pairs {ArgName, ArgValue} 
 into structure of default arguments

 02/13 Falko Schindler

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function args = parseArguments(args, varargin)
0002 % PARSEARGUMENTS parses a list of pairs {ArgName, ArgValue}
0003 % into structure of default arguments
0004 %
0005 % 02/13 Falko Schindler
0006 
0007 %% convert argument structure to argument pairs
0008 if ~isempty(varargin) && isstruct(varargin{1})
0009     arg1 = [fieldnames(varargin{1}), struct2cell(varargin{1})]';
0010     varargin = {arg1{:}, varargin{2 : end}};
0011 end
0012 
0013 %% process pairs of arguments
0014 fields = fieldnames(args);
0015 for i = 1 : 2 : numel(varargin)
0016     target = textscan(varargin{i}, '%s', 'Delimiter', '.');
0017     try
0018         getfield(args, target{1}{:});
0019     catch
0020         if numel(target{1}) == 1 && any(strcmpi(varargin{i}, fields))
0021             target = {fields(find(strcmpi(varargin{i}, fields), 1))};
0022         else
0023             error('Unknown parameter ''%s''', varargin{i});
0024         end
0025     end
0026     args = setfield(args, target{1}{:}, varargin{i + 1});
0027 end

Generated on Sat 21-Jul-2018 20:56:10 by m2html © 2005