]> Creatis software - CreaPhase.git/blob - octave_packages/struct-1.0.10/setfields.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / struct-1.0.10 / setfields.m
1 ## Copyright (C) 2000 Etienne Grossmann <etienne@egdn.net>
2 ## Copyright (C) 2000 Paul Kienzle <pkienzle@users.sf.net>
3 ## Copyright (C) 2012 Olaf Till <i7tiol@t-online.de>
4 ##
5 ## This program is free software; you can redistribute it and/or modify it under
6 ## the terms of the GNU General Public License as published by the Free Software
7 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## version.
9 ##
10 ## This program is distributed in the hope that it will be useful, but WITHOUT
11 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## details.
14 ##
15 ## You should have received a copy of the GNU General Public License along with
16 ## this program; if not, see <http://www.gnu.org/licenses/>.
17
18 ## -*- texinfo -*-
19 ## @deftypefn {Function File} {} @var{s} = setfields(@var{s},@var{key},@var{value},...)
20 ## Sets @var{s}.@var{key1} = @var{value1},  @var{s}.@var{key2} = @var{value2}, etc, finally
21 ## returning s.
22 ## For some compatibility and flexibility.
23 ## @seealso{cmpstruct, fields, rmfield, isstruct, getfields, isfield,struct}
24 ## @end deftypefn
25
26 function s = setfields (s, varargin)
27
28   if ((nargs = nargin ()) == 0)
29     s = struct ();
30   elseif (all (size (s) <= 1))
31     if (rem (nargs, 2))
32       pairs = reshape (varargin, 2, []);
33       if (! iscellstr (pairs(1, :)))
34         error ("setfields: called with non-string key");
35       endif
36       if (isempty (s))
37         s = struct (); # might have been an empty array
38       endif
39       s = cell2fields (pairs(2, :), pairs(1, :), 2, s);
40     else
41       error ("setfields: expected struct, key1, val1, key2, val2, ...");
42     endif
43   else
44     error ("structure must be scalar or empty");
45   endif
46
47 %!assert (setfields ({}, 'key', 'value'), struct ('key', 'value'));