# HG changeset patch # User Jaroslav Hajek # Date 1265900883 -3600 # Node ID 0d928dd9eeb8dc5d17003677320eb037c7e4a782 # Parent 4e4270ab70d62b330610241cb2666aea43bbad23 extend parseparams diff -r 4e4270ab70d6 -r 0d928dd9eeb8 doc/ChangeLog --- a/doc/ChangeLog Thu Feb 11 08:13:49 2010 +0100 +++ b/doc/ChangeLog Thu Feb 11 16:08:03 2010 +0100 @@ -1,3 +1,8 @@ +2010-02-11 Jaroslav Hajek + + * interpreter/expr.txi: Mention no ternary operator and the ifelse + fucntion as a possible replacement. + 2010-02-09 Jaroslav Hajek * interpreter/tips.txi: More tips. diff -r 4e4270ab70d6 -r 0d928dd9eeb8 doc/interpreter/expr.txi --- a/doc/interpreter/expr.txi Thu Feb 11 08:13:49 2010 +0100 +++ b/doc/interpreter/expr.txi Thu Feb 11 16:08:03 2010 +0100 @@ -826,6 +826,11 @@ arguments because Octave would be forced to try to evaluate both of the operands for the operator @samp{&}. +The ternary operator (?:) is not supported in Octave. If short-circuiting is +not important, it can be replaced by the @code{ifelse} function. + +@DOCSTRING(ifelse) + @node Assignment Ops @section Assignment Expressions @cindex assignment expressions diff -r 4e4270ab70d6 -r 0d928dd9eeb8 scripts/ChangeLog --- a/scripts/ChangeLog Thu Feb 11 08:13:49 2010 +0100 +++ b/scripts/ChangeLog Thu Feb 11 16:08:03 2010 +0100 @@ -1,3 +1,8 @@ +2010-02-11 Jaroslav Hajek + + * miscellaneous/parseparams.m: Extend to allow direct parsing of + options. + 2010-02-10 Jaroslav Hajek * optimization/fminbnd.m: Undocument impossible info values. diff -r 4e4270ab70d6 -r 0d928dd9eeb8 scripts/miscellaneous/parseparams.m --- a/scripts/miscellaneous/parseparams.m Thu Feb 11 08:13:49 2010 +0100 +++ b/scripts/miscellaneous/parseparams.m Thu Feb 11 16:08:03 2010 +0100 @@ -1,4 +1,5 @@ ## Copyright (C) 2006, 2007 Alexander Barth +## Copyright (C) 2010 VZLU Prague ## ## This file is part of Octave. ## @@ -18,6 +19,7 @@ ## -*- texinfo -*- ## @deftypefn {Function File} {[@var{reg}, @var{prop}] =} parseparams (@var{params}) +## @deftypefnx {Function File} {[@var{reg}, @var{var1}, @dots{}] =} parseparams (@var{params}, @var{name1}, @var{default1}, @dots{}) ## Return in @var{reg} the cell elements of @var{param} up to the first ## string element and in @var{prop} all remaining elements beginning ## with the first string element. For example @@ -41,24 +43,65 @@ ## The parseparams function may be used to separate 'regular' ## arguments and additional arguments given as property/value pairs of ## the @var{varargin} cell array. +## +## In the second form of the call, available options are specified directly +## with their default values given as name-value pairs. +## If @var{params} do not form name-value pairs, or if an option occurs +## that does not match any of the available options, an error occurs. +## When called from a m-file function, the error is prefixed with the +## name of the caller function. +## The matching of options is case-insensitive. +## ## @seealso{varargin} ## @end deftypefn ## Author: Alexander Barth ## Author: Aida Alvera Azcarate -function [reg, prop] = parseparams (params) +function [reg, varargout] = parseparams (params, varargin) - i = 1; + strs = cellfun ("isclass", params, "char"); + i = find (strs, 1); + if (i) + reg = params(1:i-1); + prop = params(i:end); + else + reg = params; + prop = {}; + endif - while (i <= numel (params)) - if (ischar (params{i})) - break; + if (nargin == 1) + varargout = {prop}; + else + names = varargin(1:2:end); + defaults = varargin(2:2:end); + if (! size_equal (names, defaults)) + error ("parseparams: needs odd number of arguments"); endif - i++; - endwhile + [names, sidx] = sort (names); - reg = params(1:i-1); - prop = params(i:end); + varargout = defaults; + if (i) + ## Let's parse the properties. + pnames = prop(1:2:end); + values = prop(2:2:end); + if (! size_equal (pnames, values) || ! all (strs(i:2:end))) + error_as_caller ("options must be given as name-value pairs"); + endif + idx = lookup (names, pnames, "mi"); + if (! all (idx)) + error_as_caller ("unrecognized option: %s", pnames{find (idx == 0, 1)}); + else + varargout(sidx(idx)) = values; + endif + endif + endif endfunction + +function error_as_caller (msg, varargin) + stack = dbstack (1); # omit me + fname = stack(min (2, end)).name; + error ([fname, ": ", msg], varargin{:}); +endfunction +