annotate scripts/miscellaneous/getfield.m @ 10122:9d1a14e12431

Update docs and add tests for container functions
author Thorsten Meyer <thorsten.meyier@gmx.de>
date Sun, 17 Jan 2010 13:31:42 +0100
parents 87fd803c583b
children 693e22af08ae
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
1 ## Copyright (C) 2000, 2006, 2007, 2009 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: 6713
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: 6713
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: 6713
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: 6713
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 -*-
6713
26f3d9bf9562 [project @ 2007-06-13 10:34:14 by dbateman]
dbateman
parents: 6547
diff changeset
21 ## @deftypefn {Function File} {[@var{v1}, @dots{}] =} getfield (@var{s}, @var{key}, @dots{})
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9849
diff changeset
22 ## Extract a field from a structure (or a nested structure). 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 ## ss(1,2).fd(3).b = 5;
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6160
diff changeset
27 ## getfield (ss, @{1,2@}, "fd", @{3@}, "b")
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9849
diff changeset
28 ## @result{} ans = 5
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
29 ## @end group
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
30 ## @end example
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
31 ##
6547
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6160
diff changeset
32 ## Note that the function call in the previous example is equivalent to
4fb053f24fd6 [project @ 2007-04-19 21:47:40 by jwe]
jwe
parents: 6160
diff changeset
33 ## the expression
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: 9036
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";
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7017
diff changeset
38 ## ss(i1@{:@}).(i2)(i3@{:@}).(i4)
10122
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9849
diff changeset
39 ## @result{} ans = 5
9d1a14e12431 Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 9849
diff changeset
40 ##
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 9036
diff changeset
41 ## @end group
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
42 ## @end example
6160
18344a472daf [project @ 2006-11-14 06:57:02 by jwe]
jwe
parents: 5820
diff changeset
43 ## @seealso{setfield, rmfield, isfield, isstruct, fieldnames, struct}
5820
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
44 ## @end deftypefn
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
45
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
46 ## Author: Etienne Grossmann <etienne@cs.uky.edu>
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
47
9849
87fd803c583b rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
48 function obj = getfield (s, varargin)
87fd803c583b rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
49 if (nargin < 2)
87fd803c583b rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
50 print_usage ();
87fd803c583b rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
51 endif
87fd803c583b rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents: 9051
diff changeset
52 subs = varargin;
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 = subsref (s, struct ("type", typs, "subs", subs));
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 ("getfield: 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
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
63 %!test
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
64 %! x.a = "hello";
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
65 %! assert(getfield(x,"a"),"hello");
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
66 %!test
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
67 %! ss(1,2).fd(3).b = 5;
27c966e4b2dc [project @ 2006-05-17 21:00:54 by jwe]
jwe
parents:
diff changeset
68 %! assert(getfield(ss,{1,2},'fd',{3},'b'),5)