mathieu@14
|
1 % PROCESS_OPTIONS - Processes options passed to a Matlab function.
|
mathieu@14
|
2 % This function provides a simple means of
|
mathieu@14
|
3 % parsing attribute-value options. Each option is
|
mathieu@14
|
4 % named by a unique string and is given a default
|
mathieu@14
|
5 % value.
|
mathieu@14
|
6 %
|
mathieu@14
|
7 % Usage: [var1, var2, ..., varn[, unused]] = ...
|
mathieu@14
|
8 % process_options(args, ...
|
mathieu@14
|
9 % str1, def1, str2, def2, ..., strn, defn)
|
mathieu@14
|
10 %
|
mathieu@14
|
11 % Arguments:
|
mathieu@14
|
12 % args - a cell array of input arguments, such
|
mathieu@14
|
13 % as that provided by VARARGIN. Its contents
|
mathieu@14
|
14 % should alternate between strings and
|
mathieu@14
|
15 % values.
|
mathieu@14
|
16 % str1, ..., strn - Strings that are associated with a
|
mathieu@14
|
17 % particular variable
|
mathieu@14
|
18 % def1, ..., defn - Default values returned if no option
|
mathieu@14
|
19 % is supplied
|
mathieu@14
|
20 % NOTE: this version (modified by dpwe) will run str2num on
|
mathieu@14
|
21 % string arguments if the default value is numeric
|
mathieu@14
|
22 % (to support passing in numbers from the command line).
|
mathieu@14
|
23 %
|
mathieu@14
|
24 % Returns:
|
mathieu@14
|
25 % var1, ..., varn - values to be assigned to variables
|
mathieu@14
|
26 % unused - an optional cell array of those
|
mathieu@14
|
27 % string-value pairs that were unused;
|
mathieu@14
|
28 % if this is not supplied, then a
|
mathieu@14
|
29 % warning will be issued for each
|
mathieu@14
|
30 % option in args that lacked a match.
|
mathieu@14
|
31 %
|
mathieu@14
|
32 % Examples:
|
mathieu@14
|
33 %
|
mathieu@14
|
34 % Suppose we wish to define a Matlab function 'func' that has
|
mathieu@14
|
35 % required parameters x and y, and optional arguments 'u' and 'v'.
|
mathieu@14
|
36 % With the definition
|
mathieu@14
|
37 %
|
mathieu@14
|
38 % function y = func(x, y, varargin)
|
mathieu@14
|
39 %
|
mathieu@14
|
40 % [u, v] = process_options(varargin, 'u', 0, 'v', 1);
|
mathieu@14
|
41 %
|
mathieu@14
|
42 % calling func(0, 1, 'v', 2) will assign 0 to x, 1 to y, 0 to u, and 2
|
mathieu@14
|
43 % to v. The parameter names are insensitive to case; calling
|
mathieu@14
|
44 % func(0, 1, 'V', 2) has the same effect. The function call
|
mathieu@14
|
45 %
|
mathieu@14
|
46 % func(0, 1, 'u', 5, 'z', 2);
|
mathieu@14
|
47 %
|
mathieu@14
|
48 % will result in u having the value 5 and v having value 1, but
|
mathieu@14
|
49 % will issue a warning that the 'z' option has not been used. On
|
mathieu@14
|
50 % the other hand, if func is defined as
|
mathieu@14
|
51 %
|
mathieu@14
|
52 % function y = func(x, y, varargin)
|
mathieu@14
|
53 %
|
mathieu@14
|
54 % [u, v, unused_args] = process_options(varargin, 'u', 0, 'v', 1);
|
mathieu@14
|
55 %
|
mathieu@14
|
56 % then the call func(0, 1, 'u', 5, 'z', 2) will yield no warning,
|
mathieu@14
|
57 % and unused_args will have the value {'z', 2}. This behaviour is
|
mathieu@14
|
58 % useful for functions with options that invoke other functions
|
mathieu@14
|
59 % with options; all options can be passed to the outer function and
|
mathieu@14
|
60 % its unprocessed arguments can be passed to the inner function.
|
mathieu@14
|
61
|
mathieu@14
|
62 % Copyright (C) 2002 Mark A. Paskin
|
mathieu@14
|
63 %
|
mathieu@14
|
64 % This program is free software; you can redistribute it and/or modify
|
mathieu@14
|
65 % it under the terms of the GNU General Public License as published by
|
mathieu@14
|
66 % the Free Software Foundation; either version 2 of the License, or
|
mathieu@14
|
67 % (at your option) any later version.
|
mathieu@14
|
68 %
|
mathieu@14
|
69 % This program is distributed in the hope that it will be useful, but
|
mathieu@14
|
70 % WITHOUT ANY WARRANTY; without even the implied warranty of
|
mathieu@14
|
71 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
mathieu@14
|
72 % General Public License for more details.
|
mathieu@14
|
73 %
|
mathieu@14
|
74 % You should have received a copy of the GNU General Public License
|
mathieu@14
|
75 % along with this program; if not, write to the Free Software
|
mathieu@14
|
76 % Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
mathieu@14
|
77 % USA.
|
mathieu@14
|
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
mathieu@14
|
79
|
mathieu@14
|
80 function [varargout] = process_options(args, varargin)
|
mathieu@14
|
81
|
mathieu@14
|
82 % Check the number of input arguments
|
mathieu@14
|
83 n = length(varargin);
|
mathieu@14
|
84 if (mod(n, 2))
|
mathieu@14
|
85 error('Each option must be a string/value pair.');
|
mathieu@14
|
86 end
|
mathieu@14
|
87
|
mathieu@14
|
88 % Check the number of supplied output arguments
|
mathieu@14
|
89 if (nargout < (n / 2))
|
mathieu@14
|
90 error('Insufficient number of output arguments given');
|
mathieu@14
|
91 elseif (nargout == (n / 2))
|
mathieu@14
|
92 warn = 1;
|
mathieu@14
|
93 nout = n / 2;
|
mathieu@14
|
94 else
|
mathieu@14
|
95 warn = 0;
|
mathieu@14
|
96 nout = n / 2 + 1;
|
mathieu@14
|
97 end
|
mathieu@14
|
98
|
mathieu@14
|
99 % Set outputs to be defaults
|
mathieu@14
|
100 varargout = cell(1, nout);
|
mathieu@14
|
101 for i=2:2:n
|
mathieu@14
|
102 varargout{i/2} = varargin{i};
|
mathieu@14
|
103 end
|
mathieu@14
|
104
|
mathieu@14
|
105 % Now process all arguments
|
mathieu@14
|
106 nunused = 0;
|
mathieu@14
|
107 for i=1:2:length(args)
|
mathieu@14
|
108 found = 0;
|
mathieu@14
|
109 for j=1:2:n
|
mathieu@14
|
110 if strcmpi(args{i}, varargin{j})
|
mathieu@14
|
111 % dpwe: promote string args to numeric if default arg is
|
mathieu@14
|
112 % numeric
|
mathieu@14
|
113 if isnumeric(varargin{j+1}) && ischar(args{i+1})
|
mathieu@14
|
114 varargout{(j + 1)/2} = str2num(args{i + 1});
|
mathieu@14
|
115 % this occurs when specifying arguments from command line
|
mathieu@14
|
116 else
|
mathieu@14
|
117 % in all other cases, take what you get
|
mathieu@14
|
118 varargout{(j + 1)/2} = args{i + 1};
|
mathieu@14
|
119 end
|
mathieu@14
|
120 found = 1;
|
mathieu@14
|
121 break;
|
mathieu@14
|
122 end
|
mathieu@14
|
123 end
|
mathieu@14
|
124 if (~found)
|
mathieu@14
|
125 if (warn)
|
mathieu@32
|
126 fprintf(2, sprintf('Option ''%s'' not used.', args{i}));
|
mathieu@14
|
127 args{i}
|
mathieu@14
|
128 else
|
mathieu@14
|
129 nunused = nunused + 1;
|
mathieu@14
|
130 unused{2 * nunused - 1} = args{i};
|
mathieu@14
|
131 if length(args) > i
|
mathieu@14
|
132 % don't demand a value for the last, unused tag (e.g. -help)
|
mathieu@14
|
133 unused{2 * nunused} = args{i + 1};
|
mathieu@14
|
134 end
|
mathieu@14
|
135 end
|
mathieu@14
|
136 end
|
mathieu@14
|
137 end
|
mathieu@14
|
138
|
mathieu@14
|
139 % Assign the unused arguments
|
mathieu@14
|
140 if (~warn)
|
mathieu@14
|
141 if (nunused)
|
mathieu@14
|
142 varargout{nout} = unused;
|
mathieu@14
|
143 else
|
mathieu@14
|
144 varargout{nout} = cell(0);
|
mathieu@14
|
145 end
|
mathieu@14
|
146 end
|