annotate rastamat/process_options.m @ 0:1b2ac32f7152

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