Dimitrios@0: function [varargout] = parse_opt(args, varargin) Dimitrios@0: % function [varargout] = parse_opt(args, varargin) Dimitrios@0: % Dimitrios@0: % Process name-value argument pairs which can be passed in arbitrary Dimitrios@0: % order. This function is very much like Mark A. Paskin's Dimitrios@0: % 'process_options' function (from which it borrows heavily), but aims Dimitrios@0: % to be a bit faster and therefore is simpler and does less error checking. Dimitrios@0: % Dimitrios@0: % Example: Suppose we pass varargin = {'a', 23, 'c', 'hi'} into parse_opt: Dimitrios@0: % Dimitrios@0: % [a,b,c] = parse_opt(varargin, 'a', 1, 'b', 2, 'c', 'test'); Dimitrios@0: % Dimitrios@0: % This would result in a=23, b=2, and c='hi' as we gave values for variables Dimitrios@0: % 'a' and 'c', but not 'b' which got the default value of 2. Dimitrios@0: % Dimitrios@0: % 2010-01-14 Graham Grindlay (grindlay@ee.columbia.edu) Dimitrios@0: Dimitrios@0: % Copyright (C) 2008-2028 Graham Grindlay (grindlay@ee.columbia.edu) Dimitrios@0: % Based on process_options.m Copyright (C) 2002 Mark A. Paskin Dimitrios@0: % This program is free software: you can redistribute it and/or modify Dimitrios@0: % it under the terms of the GNU General Public License as published by Dimitrios@0: % the Free Software Foundation, either version 3 of the License, or Dimitrios@0: % (at your option) any later version. Dimitrios@0: % Dimitrios@0: % This program is distributed in the hope that it will be useful, Dimitrios@0: % but WITHOUT ANY WARRANTY; without even the implied warranty of Dimitrios@0: % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Dimitrios@0: % GNU General Public License for more details. Dimitrios@0: % Dimitrios@0: % You should have received a copy of the GNU General Public License Dimitrios@0: % along with this program. If not, see . Dimitrios@0: Dimitrios@0: ni = length(varargin); Dimitrios@0: Dimitrios@0: % these are just about the only things that we'll check for Dimitrios@0: if nargout*2 ~= ni Dimitrios@0: error('parse_opt requires a name-value input pair for each output'); Dimitrios@0: end Dimitrios@0: if mod(ni,2) ~= 0 Dimitrios@0: error('parse_opt requires name-value input pairs'); Dimitrios@0: end Dimitrios@0: Dimitrios@0: no = ni/2; Dimitrios@0: varargout = cell(1,no); Dimitrios@0: for i = 1:2:ni Dimitrios@0: ndx = find(strcmpi(varargin{i}, args)); Dimitrios@0: if isempty(ndx) Dimitrios@0: varargout{(i+1)/2} = varargin{i+1}; Dimitrios@0: else Dimitrios@0: varargout{(i+1)/2} = args{ndx+1}; Dimitrios@0: end Dimitrios@0: end