Mercurial > hg > octave-nkf
annotate scripts/miscellaneous/setfield.m @ 11523:fd0a3ac60b0e
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 14 Jan 2011 05:47:45 -0500 |
parents | 9d1a14e12431 |
children | cefd568ea073 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2000-2011 Etienne Grossmann |
9849
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
2 ## Copyright (C) 2009 VZLU Prague |
5820 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
5820 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
5820 | 19 |
20 ## -*- texinfo -*- | |
6547 | 21 ## @deftypefn {Function File} {[@var{k1}, @dots{}, @var{v1}] =} setfield (@var{s}, @var{k1}, @var{v1}, @dots{}) |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9849
diff
changeset
|
22 ## Set a field member in a (nested) structure array. For example: |
5820 | 23 ## |
24 ## @example | |
25 ## @group | |
8507 | 26 ## oo(1,1).f0 = 1; |
27 ## oo = setfield (oo, @{1,2@}, "fd", @{3@}, "b", 6); | |
5820 | 28 ## oo(1,2).fd(3).b == 6 |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9849
diff
changeset
|
29 ## @result{} ans = 1 |
5820 | 30 ## @end group |
31 ## @end example | |
32 ## | |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9849
diff
changeset
|
33 ## Note that the same result as in the above example could be achieved by: |
5820 | 34 ## |
35 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
36 ## @group |
8507 | 37 ## i1 = @{1,2@}; i2 = "fd"; i3 = @{3@}; i4 = "b"; |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9849
diff
changeset
|
38 ## oo(i1@{:@}).(i2)(i3@{:@}).(i4) == 6 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9849
diff
changeset
|
39 ## @result{} ans = 1 |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
40 ## @end group |
5820 | 41 ## @end example |
42 ## @seealso{getfield, rmfield, isfield, isstruct, fieldnames, struct} | |
43 ## @end deftypefn | |
44 | |
45 ## Author: Etienne Grossmann <etienne@cs.uky.edu> | |
46 | |
47 function obj = setfield (obj, varargin) | |
9849
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
48 if (nargin < 3) |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
49 print_usage (); |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
50 endif |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
51 subs = varargin(1:end-1); |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
52 rhs = varargin{end}; |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
53 flds = cellfun (@ischar, subs); |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
54 idxs = cellfun (@iscell, subs); |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
55 if (all (flds | idxs)) |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
56 typs = merge (flds, {"."}, {"()"}); |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
57 obj = subsasgn (obj, struct ("type", typs, "subs", subs), rhs); |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
58 else |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
59 error ("setfield: invalid index"); |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
60 endif |
5820 | 61 endfunction |
62 | |
63 %!test | |
64 %! x.a = "hello"; | |
65 %! x = setfield(x,"b","world"); | |
8507 | 66 %! y = struct("a","hello","b","world"); |
5820 | 67 %! assert(x,y); |
68 %!test | |
69 %! oo(1,1).f0= 1; | |
8507 | 70 %! oo = setfield(oo,{1,2},"fd",{3},"b", 6); |
5820 | 71 %! assert (oo(1,2).fd(3).b, 6) |