X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?p=CreaPhase.git;a=blobdiff_plain;f=octave_packages%2Fstruct-1.0.10%2Fsetfields.m;fp=octave_packages%2Fstruct-1.0.10%2Fsetfields.m;h=a6d90a243c16090e5a4219ca224596c6fa35cfc3;hp=0000000000000000000000000000000000000000;hb=c880e8788dfc484bf23ce13fa2787f2c6bca4863;hpb=1705066eceaaea976f010f669ce8e972f3734b05 diff --git a/octave_packages/struct-1.0.10/setfields.m b/octave_packages/struct-1.0.10/setfields.m new file mode 100644 index 0000000..a6d90a2 --- /dev/null +++ b/octave_packages/struct-1.0.10/setfields.m @@ -0,0 +1,47 @@ +## Copyright (C) 2000 Etienne Grossmann +## Copyright (C) 2000 Paul Kienzle +## Copyright (C) 2012 Olaf Till +## +## This program is free software; you can redistribute it and/or modify it under +## the terms of the GNU General Public License as published by the Free Software +## Foundation; either version 3 of the License, or (at your option) any later +## version. +## +## This program is distributed in the hope that it will be useful, but WITHOUT +## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +## details. +## +## You should have received a copy of the GNU General Public License along with +## this program; if not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} {} @var{s} = setfields(@var{s},@var{key},@var{value},...) +## Sets @var{s}.@var{key1} = @var{value1}, @var{s}.@var{key2} = @var{value2}, etc, finally +## returning s. +## For some compatibility and flexibility. +## @seealso{cmpstruct, fields, rmfield, isstruct, getfields, isfield,struct} +## @end deftypefn + +function s = setfields (s, varargin) + + if ((nargs = nargin ()) == 0) + s = struct (); + elseif (all (size (s) <= 1)) + if (rem (nargs, 2)) + pairs = reshape (varargin, 2, []); + if (! iscellstr (pairs(1, :))) + error ("setfields: called with non-string key"); + endif + if (isempty (s)) + s = struct (); # might have been an empty array + endif + s = cell2fields (pairs(2, :), pairs(1, :), 2, s); + else + error ("setfields: expected struct, key1, val1, key2, val2, ..."); + endif + else + error ("structure must be scalar or empty"); + endif + +%!assert (setfields ({}, 'key', 'value'), struct ('key', 'value'));