1 ## Copyright (C) 2006-2012 Alexander Barth
2 ## Copyright (C) 2010 VZLU Prague
4 ## This file is part of Octave.
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ## General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>.
21 ## @deftypefn {Function File} {[@var{reg}, @var{prop}] =} parseparams (@var{params})
22 ## @deftypefnx {Function File} {[@var{reg}, @var{var1}, @dots{}] =} parseparams (@var{params}, @var{name1}, @var{default1}, @dots{})
23 ## Return in @var{reg} the cell elements of @var{param} up to the first
24 ## string element and in @var{prop} all remaining elements beginning
25 ## with the first string element. For example:
29 ## [reg, prop] = parseparams (@{1, 2, "linewidth", 10@})
43 ## The parseparams function may be used to separate 'regular'
44 ## arguments and additional arguments given as property/value pairs of
45 ## the @var{varargin} cell array.
47 ## In the second form of the call, available options are specified directly
48 ## with their default values given as name-value pairs.
49 ## If @var{params} do not form name-value pairs, or if an option occurs
50 ## that does not match any of the available options, an error occurs.
51 ## When called from an m-file function, the error is prefixed with the
52 ## name of the caller function.
53 ## The matching of options is case-insensitive.
58 ## Author: Alexander Barth <abarth93@users.sourceforge.net>
59 ## Author: Aida Alvera Azcarate <aida@netecho.info>
61 function [reg, varargout] = parseparams (params, varargin)
63 strs = cellfun ("isclass", params, "char");
76 names = varargin(1:2:end);
77 defaults = varargin(2:2:end);
78 if (! size_equal (names, defaults))
79 error ("parseparams: needs odd number of arguments");
81 [names, sidx] = sort (names);
85 ## Let's parse the properties.
86 pnames = prop(1:2:end);
87 values = prop(2:2:end);
88 if (! size_equal (pnames, values) || ! all (strs(i:2:end)))
89 error_as_caller ("options must be given as name-value pairs");
91 idx = lookup (toupper(names), toupper(pnames), "m");
93 error_as_caller ("unrecognized option: %s", pnames{find (idx == 0, 1)});
95 varargout(sidx(idx)) = values;
102 function error_as_caller (msg, varargin)
103 stack = dbstack (1); # omit me
104 fname = stack(min (2, end)).name;
105 error ([fname, ": ", msg], varargin{:});