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 -*- |
|
21 ## @deftypefn {Built-in Function} {} [@var{v1},...] = |
|
22 ## @code{getfield (@var{s}, 'k1',...)} extract fields from a structure. |
|
23 ## For example |
|
24 ## |
|
25 ## @example |
|
26 ## @group |
|
27 ## ss(1,2).fd(3).b=5; |
|
28 ## getfield(ss,@{1,2@},'fd',@{3@},'b') |
|
29 ## @result{} ans = 5 |
|
30 ## @end group |
|
31 ## @end example |
|
32 ## |
|
33 ## Note that this function could be written as |
|
34 ## |
|
35 ## @example |
|
36 ## i1= @{1,2@}; i2= 'fd'; i3= @{3@}; i4= 'b'; |
|
37 ## ss( i1@{:@} ).( i2 )( i3@{:@} ).( i4 ) |
|
38 ## @end example |
|
39 ## @seealso{setfield,rmfield,isfield,isstruct,fields,struct} |
|
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) |