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