Mercurial > octave
annotate scripts/miscellaneous/getfield.m @ 27919:1891570abac8
update Octave Project Developers copyright for the new year
In files that have the "Octave Project Developers" copyright notice,
update for 2020.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 06 Jan 2020 22:29:51 -0500 |
parents | b442ec6dda5c |
children | bd51beb6205e |
rev | line source |
---|---|
27919
1891570abac8
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27918
diff
changeset
|
1 ## Copyright (C) 2000-2020 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
2 ## |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
3 ## See the file COPYRIGHT.md in the top-level directory of this distribution |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
4 ## or <https://octave.org/COPYRIGHT.html/>. |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
5 ## |
5820 | 6 ## |
7 ## This file is part of Octave. | |
8 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
9 ## Octave is free software: you can redistribute it and/or modify it |
5820 | 10 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
11 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
12 ## (at your option) any later version. |
5820 | 13 ## |
14 ## Octave is distributed in the hope that it will be useful, but | |
15 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## GNU General Public License for more details. |
5820 | 18 ## |
19 ## You should have received a copy of the GNU General Public License | |
7016 | 20 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
21 ## <https://www.gnu.org/licenses/>. |
5820 | 22 |
23 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
24 ## @deftypefn {} {@var{val} =} getfield (@var{s}, @var{field}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
25 ## @deftypefnx {} {@var{val} =} getfield (@var{s}, @var{sidx1}, @var{field1}, @var{fidx1}, @dots{}) |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
26 ## 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
|
27 ## structure @var{s}. |
5820 | 28 ## |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
29 ## 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
|
30 ## 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
|
31 ## 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
|
32 ## 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
|
33 ## description of the syntax. |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
34 ## |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
35 ## @seealso{setfield, rmfield, orderfields, isfield, fieldnames, isstruct, struct} |
5820 | 36 ## @end deftypefn |
37 | |
38 ## Author: Etienne Grossmann <etienne@cs.uky.edu> | |
39 | |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
40 function val = getfield (s, varargin) |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
41 |
9849
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
42 if (nargin < 2) |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
43 print_usage (); |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
44 endif |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
45 |
9849
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
46 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
|
47 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
|
48 idxs = cellfun ("isclass", subs, "cell"); |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
49 if (! all (flds | idxs)) |
9849
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
50 error ("getfield: invalid index"); |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
51 endif |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
52 |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
53 typs = merge (flds, {"."}, {"()"}); |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
54 val = subsref (s, struct ("type", typs, "subs", subs)); |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
55 |
5820 | 56 endfunction |
57 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
58 |
5820 | 59 %!test |
60 %! x.a = "hello"; | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
61 %! assert (getfield (x, "a"), "hello"); |
5820 | 62 %!test |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
63 %! ss(1,2).fd(3).b(1,4) = 5; |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
64 %! 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
|
65 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
66 ## Test input validation |
19276
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
67 %!error getfield () |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
68 %!error getfield (1) |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18533
diff
changeset
|
69 %!error <invalid index> getfield (1,2) |