annotate toolboxes/distance_learning/mlr/util/parsepvpairs.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function varargout = parsepvpairs(names, defaults, varargin)
wolffd@0 2 %PARSEPVPAIRS Validate parameter name/value pairs and throw errors if necessary.
wolffd@0 3 % Given the cell array of valid parameter names, a corresponding cell array of
wolffd@0 4 % parameter default values, and a variable length list of parameter name/value
wolffd@0 5 % pairs, validate the specified name/value pairs and assign values to output
wolffd@0 6 % parameters.
wolffd@0 7 %
wolffd@0 8 % [P1, P2, ...] = parsepvpairs(Names, Defaults, 'Name1', Value1, 'Name2', Value2, ...)
wolffd@0 9 %
wolffd@0 10 % Inputs:
wolffd@0 11 % Names - Cell array of valid parameter names.
wolffd@0 12 %
wolffd@0 13 % Defaults - Cell array of default values for the parameters named in Names.
wolffd@0 14 %
wolffd@0 15 % Name# - Character strings of parameter names to be validated and
wolffd@0 16 % assigned the corresponding value that immediately follows each
wolffd@0 17 % in the input argument list. Parameter name validation is
wolffd@0 18 % case-insensitive and partial string matches are allowed provided
wolffd@0 19 % no ambiguities exist.
wolffd@0 20 %
wolffd@0 21 % Value# - The values assigned to the corresponding parameter that
wolffd@0 22 % immediately precede each in the input argument list.
wolffd@0 23 %
wolffd@0 24 % Outputs:
wolffd@0 25 % P# - Parameters assigned the parameter values Value1, Value2, ...
wolffd@0 26 % in the same order as the names listed in Names. Parameters
wolffd@0 27 % corresponding to entries in Names that are not specified in the
wolffd@0 28 % name/value pairs are set to the corresponding value listed in
wolffd@0 29 % Defaults.
wolffd@0 30
wolffd@0 31 % Copyright 1995-2007 The MathWorks, Inc.
wolffd@0 32 % $Revision: 1.1.6.2 $ $Date: 2008/12/21 01:51:18 $
wolffd@0 33
wolffd@0 34 %
wolffd@0 35 % Short-circuit the input argument checking for performance purposes. Under the
wolffd@0 36 % following specific circumstances, users may by-pass P-V pair validation when:
wolffd@0 37 %
wolffd@0 38 % (1) Values are assigned to all recognized parameters,
wolffd@0 39 % (2) Parameters are specified in exactly the same order as in the input NAMES,
wolffd@0 40 % (3) All parameters are completely specified (i.e., exact matches and no partial
wolffd@0 41 % names allowed).
wolffd@0 42 %
wolffd@0 43
wolffd@0 44 if isequal(varargin(1:2:end-1), names)
wolffd@0 45 varargout = varargin(2:2:end);
wolffd@0 46 return
wolffd@0 47 end
wolffd@0 48
wolffd@0 49 % Initialize some variables.
wolffd@0 50 nInputs = length(varargin); % # of input arguments
wolffd@0 51 varargout = defaults;
wolffd@0 52
wolffd@0 53 % Ensure parameter/value pairs.
wolffd@0 54 if mod(nInputs, 2) ~= 0
wolffd@0 55 error('finance:parsepvpairs:incorrectNumberOfInputs', ...
wolffd@0 56 'Input parameters must be in name/value pair format.');
wolffd@0 57
wolffd@0 58 else
wolffd@0 59 names = lower(names);
wolffd@0 60
wolffd@0 61 % Process p/v pairs.
wolffd@0 62 for j = 1:2:nInputs
wolffd@0 63 pName = varargin{j};
wolffd@0 64
wolffd@0 65 if ~ischar(pName)
wolffd@0 66 error('finance:parsepvpairs:nonTextString', ...
wolffd@0 67 'Parameter names must be character strings.');
wolffd@0 68 end
wolffd@0 69
wolffd@0 70 i = strmatch(lower(pName), names);
wolffd@0 71
wolffd@0 72 if isempty(i)
wolffd@0 73 error('finance:parsepvpairs:invalidParameter', ...
wolffd@0 74 'Invalid parameter name: %s.', pName);
wolffd@0 75
wolffd@0 76 elseif length(i) > 1
wolffd@0 77 % If ambiguities exist, check for exact match to narrow search.
wolffd@0 78 i = strmatch(lower(pName), names, 'exact');
wolffd@0 79 if length(i) == 1
wolffd@0 80 varargout{i} = varargin{j+1};
wolffd@0 81
wolffd@0 82 else
wolffd@0 83 error('finance:parsepvpairs:ambiguousParameter', ...
wolffd@0 84 'Ambiguous parameter name: %s.', pName);
wolffd@0 85 end
wolffd@0 86
wolffd@0 87 else
wolffd@0 88 varargout{i} = varargin{j+1};
wolffd@0 89 end
wolffd@0 90 end
wolffd@0 91 end
wolffd@0 92
wolffd@0 93
wolffd@0 94 % [EOF]