Mercurial > hg > octave-nkf
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 |
rev | line source |
---|---|
8920 | 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 | 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 -*- | |
6713 | 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 | 23 ## |
24 ## @example | |
25 ## @group | |
8507 | 26 ## ss(1,2).fd(3).b = 5; |
6547 | 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 | 29 ## @end group |
30 ## @end example | |
31 ## | |
6547 | 32 ## Note that the function call in the previous example is equivalent to |
33 ## the expression | |
5820 | 34 ## |
35 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
36 ## @group |
8507 | 37 ## i1 = @{1,2@}; i2 = "fd"; i3 = @{3@}; i4= "b"; |
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 | 42 ## @end example |
6160 | 43 ## @seealso{setfield, rmfield, isfield, isstruct, fieldnames, struct} |
5820 | 44 ## @end deftypefn |
45 | |
46 ## Author: Etienne Grossmann <etienne@cs.uky.edu> | |
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 | 61 endfunction |
62 | |
63 %!test | |
64 %! x.a = "hello"; | |
65 %! assert(getfield(x,"a"),"hello"); | |
66 %!test | |
67 %! ss(1,2).fd(3).b = 5; | |
68 %! assert(getfield(ss,{1,2},'fd',{3},'b'),5) |