]> Creatis software - CreaPhase.git/blob - octave_packages/general-1.3.1/@dict/get.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / general-1.3.1 / @dict / get.m
1 ## Copyright (C) 2009 VZLU Prague, a.s., Czech Republic
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn{Function File} {} get (d, key, defv)
18 ## Queries for the values of specified key(s). Unlike indexing, however,
19 ## this does not throw an error if a key is missing but rather substitutes
20 ## a default value. If @var{key} is a cell array, @var{defv} should be either
21 ## a cell array of the same shape as @var{key}, or a singleton cell.
22 ## Non-cell values will be converted to a singleton cell.
23 ## @end deftypefn
24
25 ## Author: Jaroslav Hajek <highegg@gmail.com>
26
27 function val = get (d, key, defv = [])
28   if (nargin < 2 || nargin > 3)
29     print_usage ();
30   endif
31
32   if (ischar (key))
33     i = lookup (d.keys, key, "m");
34     if (i)
35       val = d.values{i};
36     else
37       val = defv;
38     endif
39   elseif (iscellstr (key))
40     if (! iscell (defv))
41       val = repmat ({defv}, size (key));
42     elseif (numel (defv) == 1)
43       val = repmat (defv, size (key));
44     elseif (size_equal (key, defv))
45       val = defv;
46     else
47       error ("get: sizes of key & defv must match");
48     endif
49     i = lookup (d.keys, key, "m");
50     mask = i != 0;
51     val(mask) = d.values(i(mask));
52   else
53     error ("get: invalid key value");
54   endif
55 endfunction
56