wolffd@0: function varargout = parsepvpairs(names, defaults, varargin) wolffd@0: %PARSEPVPAIRS Validate parameter name/value pairs and throw errors if necessary. wolffd@0: % Given the cell array of valid parameter names, a corresponding cell array of wolffd@0: % parameter default values, and a variable length list of parameter name/value wolffd@0: % pairs, validate the specified name/value pairs and assign values to output wolffd@0: % parameters. wolffd@0: % wolffd@0: % [P1, P2, ...] = parsepvpairs(Names, Defaults, 'Name1', Value1, 'Name2', Value2, ...) wolffd@0: % wolffd@0: % Inputs: wolffd@0: % Names - Cell array of valid parameter names. wolffd@0: % wolffd@0: % Defaults - Cell array of default values for the parameters named in Names. wolffd@0: % wolffd@0: % Name# - Character strings of parameter names to be validated and wolffd@0: % assigned the corresponding value that immediately follows each wolffd@0: % in the input argument list. Parameter name validation is wolffd@0: % case-insensitive and partial string matches are allowed provided wolffd@0: % no ambiguities exist. wolffd@0: % wolffd@0: % Value# - The values assigned to the corresponding parameter that wolffd@0: % immediately precede each in the input argument list. wolffd@0: % wolffd@0: % Outputs: wolffd@0: % P# - Parameters assigned the parameter values Value1, Value2, ... wolffd@0: % in the same order as the names listed in Names. Parameters wolffd@0: % corresponding to entries in Names that are not specified in the wolffd@0: % name/value pairs are set to the corresponding value listed in wolffd@0: % Defaults. wolffd@0: wolffd@0: % Copyright 1995-2007 The MathWorks, Inc. wolffd@0: % $Revision: 1.1.6.2 $ $Date: 2008/12/21 01:51:18 $ wolffd@0: wolffd@0: % wolffd@0: % Short-circuit the input argument checking for performance purposes. Under the wolffd@0: % following specific circumstances, users may by-pass P-V pair validation when: wolffd@0: % wolffd@0: % (1) Values are assigned to all recognized parameters, wolffd@0: % (2) Parameters are specified in exactly the same order as in the input NAMES, wolffd@0: % (3) All parameters are completely specified (i.e., exact matches and no partial wolffd@0: % names allowed). wolffd@0: % wolffd@0: wolffd@0: if isequal(varargin(1:2:end-1), names) wolffd@0: varargout = varargin(2:2:end); wolffd@0: return wolffd@0: end wolffd@0: wolffd@0: % Initialize some variables. wolffd@0: nInputs = length(varargin); % # of input arguments wolffd@0: varargout = defaults; wolffd@0: wolffd@0: % Ensure parameter/value pairs. wolffd@0: if mod(nInputs, 2) ~= 0 wolffd@0: error('finance:parsepvpairs:incorrectNumberOfInputs', ... wolffd@0: 'Input parameters must be in name/value pair format.'); wolffd@0: wolffd@0: else wolffd@0: names = lower(names); wolffd@0: wolffd@0: % Process p/v pairs. wolffd@0: for j = 1:2:nInputs wolffd@0: pName = varargin{j}; wolffd@0: wolffd@0: if ~ischar(pName) wolffd@0: error('finance:parsepvpairs:nonTextString', ... wolffd@0: 'Parameter names must be character strings.'); wolffd@0: end wolffd@0: wolffd@0: i = strmatch(lower(pName), names); wolffd@0: wolffd@0: if isempty(i) wolffd@0: error('finance:parsepvpairs:invalidParameter', ... wolffd@0: 'Invalid parameter name: %s.', pName); wolffd@0: wolffd@0: elseif length(i) > 1 wolffd@0: % If ambiguities exist, check for exact match to narrow search. wolffd@0: i = strmatch(lower(pName), names, 'exact'); wolffd@0: if length(i) == 1 wolffd@0: varargout{i} = varargin{j+1}; wolffd@0: wolffd@0: else wolffd@0: error('finance:parsepvpairs:ambiguousParameter', ... wolffd@0: 'Ambiguous parameter name: %s.', pName); wolffd@0: end wolffd@0: wolffd@0: else wolffd@0: varargout{i} = varargin{j+1}; wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: wolffd@0: % [EOF]