annotate scripts/miscellaneous/setfield.m @ 14459:a22a41ab6824

doc: Add Colin Macdonald and Mike Miller to contributors.in
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Tue, 13 Mar 2012 01:30:26 -0400
parents f3d52523cde1
children 808e4f13e220
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14138
72c96de7a403 maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents: 12931
diff changeset
1 ## Copyright (C) 2000-2012 Etienne Grossmann
9849
87fd803c583b rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
2 ## Copyright (C) 2009 VZLU Prague
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
3 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
4 ## This file is part of Octave.
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
5 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
9 ## your option) any later version.
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
10 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
14 ## General Public License for more details.
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
15 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
18 ## <http://www.gnu.org/licenses/>.
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
19
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
20 ## -*- texinfo -*-
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6157
diff changeset
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
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
23 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
24 ## @example
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
25 ## @group
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
26 ## oo(1,1).f0 = 1;
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
27 ## oo = setfield (oo, @{1,2@}, "fd", @{3@}, "b", 6);
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
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
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
30 ## @end group
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
31 ## @end example
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
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
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
34 ##
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
35 ## @example
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
36 ## @group
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
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
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
41 ## @end example
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
42 ## @seealso{getfield, rmfield, isfield, isstruct, fieldnames, struct}
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
43 ## @end deftypefn
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
44
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
45 ## Author: Etienne Grossmann <etienne@cs.uky.edu>
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
46
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
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};
12931
cefd568ea073 Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
53 flds = cellfun ("isclass", subs, "char");
cefd568ea073 Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
54 idxs = cellfun ("isclass", subs, "cell");
9849
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
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
61 endfunction
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
62
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
63
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
64 %!test
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
65 %! x.a = "hello";
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
66 %! x = setfield (x, "b", "world");
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
67 %! y = struct ("a", "hello", "b", "world");
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
68 %! assert (x,y);
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
69 %!test
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
70 %! oo(1,1).f0 = 1;
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
71 %! oo = setfield (oo,{1,2},"fd",{3},"b", 6);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
72 %! assert (oo(1,2).fd(3).b, 6);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
73