Mercurial > hg > octave-nkf
annotate scripts/miscellaneous/setfield.m @ 12007:dc56a38b5a64 release-3-2-x
var.m: fix typos (thinkos?) in previous change
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 23 Jun 2009 12:57:57 +0200 |
parents | 1bf0ce0930be |
children | 87fd803c583b |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2006, 2007, 2009 Etienne Grossmann |
5820 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
5820 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5820 | 18 |
19 ## -*- texinfo -*- | |
6547 | 20 ## @deftypefn {Function File} {[@var{k1}, @dots{}, @var{v1}] =} setfield (@var{s}, @var{k1}, @var{v1}, @dots{}) |
5881 | 21 ## Set field members in a structure. |
5820 | 22 ## |
23 ## @example | |
24 ## @group | |
8507 | 25 ## oo(1,1).f0 = 1; |
26 ## oo = setfield (oo, @{1,2@}, "fd", @{3@}, "b", 6); | |
5820 | 27 ## oo(1,2).fd(3).b == 6 |
28 ## @result{} ans = 1 | |
29 ## @end group | |
30 ## @end example | |
31 ## | |
32 ## Note that this function could be written | |
33 ## | |
34 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
35 ## @group |
8507 | 36 ## i1 = @{1,2@}; i2 = "fd"; i3 = @{3@}; i4 = "b"; |
37 ## oo(i1@{:@}).(i2)(i3@{:@}).(i4) == 6; | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
38 ## @end group |
5820 | 39 ## @end example |
40 ## @seealso{getfield, rmfield, isfield, isstruct, fieldnames, struct} | |
41 ## @end deftypefn | |
42 | |
43 ## Author: Etienne Grossmann <etienne@cs.uky.edu> | |
44 | |
45 function obj = setfield (obj, varargin) | |
46 field = "obj"; | |
47 for i = 1:nargin-2 | |
48 v = varargin{i}; | |
49 if (iscell (v)) | |
50 sep = "("; | |
51 for j = 1:length (v) | |
52 field = sprintf ("%s%s%s", field, sep, num2str (v{j})); | |
53 sep = ","; | |
54 endfor | |
55 field = sprintf ("%s)", field); | |
56 else | |
57 field = sprintf ("%s.%s", field, v); | |
58 endif | |
59 endfor | |
60 val = varargin{nargin-1}; | |
61 eval (sprintf ("%s=val;", field)); | |
62 endfunction | |
63 | |
64 %!test | |
65 %! x.a = "hello"; | |
66 %! x = setfield(x,"b","world"); | |
8507 | 67 %! y = struct("a","hello","b","world"); |
5820 | 68 %! assert(x,y); |
69 %!test | |
70 %! oo(1,1).f0= 1; | |
8507 | 71 %! oo = setfield(oo,{1,2},"fd",{3},"b", 6); |
5820 | 72 %! assert (oo(1,2).fd(3).b, 6) |