annotate _FullBNT/KPMtools/process_options.m @ 9:4ea6619cb3f5 tip

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