annotate parse_opt.m @ 1:3ea8ed09af0f tip

additional clarifications
author Dimitrios Giannoulis
date Wed, 13 Mar 2013 11:57:24 +0000
parents 22b10c5b72e8
children
rev   line source
Dimitrios@0 1 function [varargout] = parse_opt(args, varargin)
Dimitrios@0 2 % function [varargout] = parse_opt(args, varargin)
Dimitrios@0 3 %
Dimitrios@0 4 % Process name-value argument pairs which can be passed in arbitrary
Dimitrios@0 5 % order. This function is very much like Mark A. Paskin's
Dimitrios@0 6 % 'process_options' function (from which it borrows heavily), but aims
Dimitrios@0 7 % to be a bit faster and therefore is simpler and does less error checking.
Dimitrios@0 8 %
Dimitrios@0 9 % Example: Suppose we pass varargin = {'a', 23, 'c', 'hi'} into parse_opt:
Dimitrios@0 10 %
Dimitrios@0 11 % [a,b,c] = parse_opt(varargin, 'a', 1, 'b', 2, 'c', 'test');
Dimitrios@0 12 %
Dimitrios@0 13 % This would result in a=23, b=2, and c='hi' as we gave values for variables
Dimitrios@0 14 % 'a' and 'c', but not 'b' which got the default value of 2.
Dimitrios@0 15 %
Dimitrios@0 16 % 2010-01-14 Graham Grindlay (grindlay@ee.columbia.edu)
Dimitrios@0 17
Dimitrios@0 18 % Copyright (C) 2008-2028 Graham Grindlay (grindlay@ee.columbia.edu)
Dimitrios@0 19 % Based on process_options.m Copyright (C) 2002 Mark A. Paskin
Dimitrios@0 20 % This program is free software: you can redistribute it and/or modify
Dimitrios@0 21 % it under the terms of the GNU General Public License as published by
Dimitrios@0 22 % the Free Software Foundation, either version 3 of the License, or
Dimitrios@0 23 % (at your option) any later version.
Dimitrios@0 24 %
Dimitrios@0 25 % This program is distributed in the hope that it will be useful,
Dimitrios@0 26 % but WITHOUT ANY WARRANTY; without even the implied warranty of
Dimitrios@0 27 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Dimitrios@0 28 % GNU General Public License for more details.
Dimitrios@0 29 %
Dimitrios@0 30 % You should have received a copy of the GNU General Public License
Dimitrios@0 31 % along with this program. If not, see <http://www.gnu.org/licenses/>.
Dimitrios@0 32
Dimitrios@0 33 ni = length(varargin);
Dimitrios@0 34
Dimitrios@0 35 % these are just about the only things that we'll check for
Dimitrios@0 36 if nargout*2 ~= ni
Dimitrios@0 37 error('parse_opt requires a name-value input pair for each output');
Dimitrios@0 38 end
Dimitrios@0 39 if mod(ni,2) ~= 0
Dimitrios@0 40 error('parse_opt requires name-value input pairs');
Dimitrios@0 41 end
Dimitrios@0 42
Dimitrios@0 43 no = ni/2;
Dimitrios@0 44 varargout = cell(1,no);
Dimitrios@0 45 for i = 1:2:ni
Dimitrios@0 46 ndx = find(strcmpi(varargin{i}, args));
Dimitrios@0 47 if isempty(ndx)
Dimitrios@0 48 varargout{(i+1)/2} = varargin{i+1};
Dimitrios@0 49 else
Dimitrios@0 50 varargout{(i+1)/2} = args{ndx+1};
Dimitrios@0 51 end
Dimitrios@0 52 end