5820
|
1 ## Copyright (C) 2000 Etienne Grossmann |
|
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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
6713
|
21 ## @deftypefn {Function File} {[@var{v1}, @dots{}] =} getfield (@var{s}, @var{key}, @dots{}) |
|
22 ## Extract fields from a structure. For example |
5820
|
23 ## |
|
24 ## @example |
|
25 ## @group |
|
26 ## ss(1,2).fd(3).b=5; |
6547
|
27 ## getfield (ss, @{1,2@}, "fd", @{3@}, "b") |
5820
|
28 ## @result{} ans = 5 |
|
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 |
6547
|
36 ## i1= @{1,2@}; i2= "fd"; i3= @{3@}; i4= "b"; |
|
37 ## ss(i1@{:@}).(i2)(i3@{:@}).(i4) |
5820
|
38 ## @end example |
6160
|
39 ## @seealso{setfield, rmfield, isfield, isstruct, fieldnames, struct} |
5820
|
40 ## @end deftypefn |
|
41 |
|
42 ## Author: Etienne Grossmann <etienne@cs.uky.edu> |
|
43 |
|
44 function s = getfield (s, varargin) |
|
45 |
|
46 for idx = 1:nargin-1 |
|
47 i = varargin{idx}; |
|
48 if (iscell (i)) |
|
49 s = s(i{:}); |
|
50 else |
|
51 s = s.(i); |
|
52 endif |
|
53 endfor |
|
54 |
|
55 endfunction |
|
56 |
|
57 %!test |
|
58 %! x.a = "hello"; |
|
59 %! assert(getfield(x,"a"),"hello"); |
|
60 %!test |
|
61 %! ss(1,2).fd(3).b = 5; |
|
62 %! assert(getfield(ss,{1,2},'fd',{3},'b'),5) |