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