Mercurial > octave-antonio
annotate scripts/miscellaneous/getfield.m @ 19833:9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Try to trim long lines to < 80 chars.
Use '##' for single line comments.
Use '(...)' around tests for if/elseif/switch/while.
Abut cell indexing operator '{' next to variable.
Abut array indexing operator '(' next to variable.
Use space between negation operator '!' and following expression.
Use two newlines between endfunction and start of %!test or %!demo code.
Remove unnecessary parens grouping between short-circuit operators.
Remove stray extra spaces (typos) between variables and assignment operators.
Remove stray extra spaces from ends of lines.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 23 Feb 2015 14:54:39 -0800 |
parents | 4197fc428c7d |
children |
rev | line source |
---|---|
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19276
diff
changeset
|
1 ## Copyright (C) 2000-2015 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 -*- | |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
21 ## @deftypefn {Function File} {@var{val} =} getfield (@var{s}, @var{field}) |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
22 ## @deftypefnx {Function File} {@var{val} =} getfield (@var{s}, @var{sidx1}, @var{field1}, @var{fidx1}, @dots{}) |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
23 ## Get the value of the field named @var{field} from a structure or nested |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
24 ## structure @var{s}. |
5820 | 25 ## |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
26 ## If @var{s} is a structure array then @var{sidx} selects an element of the |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
27 ## structure array, @var{field} specifies the field name of the selected |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
28 ## element, and @var{fidx} selects which element of the field (in the case of |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
29 ## an array or cell array). See @code{setfield} for a more complete |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
30 ## description of the syntax. |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
31 ## |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
32 ## @seealso{setfield, rmfield, orderfields, isfield, fieldnames, isstruct, struct} |
5820 | 33 ## @end deftypefn |
34 | |
35 ## Author: Etienne Grossmann <etienne@cs.uky.edu> | |
36 | |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
37 function val = getfield (s, varargin) |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
38 |
9849
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
39 if (nargin < 2) |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
40 print_usage (); |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
41 endif |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
42 |
9849
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
43 subs = varargin; |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
44 flds = cellfun ("isclass", subs, "char"); |
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
45 idxs = cellfun ("isclass", subs, "cell"); |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
46 if (! all (flds | idxs)) |
9849
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
47 error ("getfield: invalid index"); |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
48 endif |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
49 |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
50 typs = merge (flds, {"."}, {"()"}); |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
51 val = subsref (s, struct ("type", typs, "subs", subs)); |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
52 |
5820 | 53 endfunction |
54 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
55 |
5820 | 56 %!test |
57 %! x.a = "hello"; | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
58 %! assert (getfield (x, "a"), "hello"); |
5820 | 59 %!test |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
60 %! ss(1,2).fd(3).b(1,4) = 5; |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
61 %! assert (getfield (ss,{1,2},"fd",{3},"b", {1,4}), 5); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
62 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
63 ## Test input validation |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
64 %!error getfield () |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
65 %!error getfield (1) |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
66 %!error <invalid index> getfield (1,2) |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
67 |