Mercurial > octave-nkf
comparison scripts/miscellaneous/getfield.m @ 19310:1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
* ov-struct.cc (Fstruct): Add seealso link to isfield in docstring.
* ov-struct.cc (Frmfield): Add seealso link to isfield in docstring.
* ov-struct.cc (Fisfield): Add newline between first sentence description
of function and remainder of docstring.
* getfield.m: Redo docstring. Match function variable names to documentation
names. Place input validation first in function. Add input validation BIST
tests.
* orderfields.m: Redo docstring. Clarify error() messages. Add additional
error message if second argument is not of the required type. Return an
empty struct of the same size as the empty struct input. Add input
validation BIST tests.
* setfield.m: Redo docstring. Match function variable names to documentation
names. Place input validation first in function. Add input validation BIST
tests.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 21 Oct 2014 09:57:02 -0700 |
parents | ea0d4dea1a17 |
children | 4197fc428c7d |
comparison
equal
deleted
inserted
replaced
19309:653ed3a6ba83 | 19310:1fa328da03ef |
---|---|
16 ## You should have received a copy of the GNU General Public License | 16 ## You should have received a copy of the GNU General Public License |
17 ## along with Octave; see the file COPYING. If not, see | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | 18 ## <http://www.gnu.org/licenses/>. |
19 | 19 |
20 ## -*- texinfo -*- | 20 ## -*- texinfo -*- |
21 ## @deftypefn {Function File} {[@var{val}] =} getfield (@var{s}, @var{field}) | 21 ## @deftypefn {Function File} {@var{val} =} getfield (@var{s}, @var{field}) |
22 ## @deftypefnx {Function File} {[@var{val}] =} getfield (@var{s}, @var{idx1}, @var{field1}, @var{idx2}, @var{field2}, @dots{}) | 22 ## @deftypefnx {Function File} {@var{val} =} getfield (@var{s}, @var{sidx1}, @var{field1}, @var{fidx1}, @dots{}) |
23 ## Extract a field from a structure (or a nested structure). The syntax | 23 ## Get the value of the field named @var{field} from a structure or nested |
24 ## is the same as @code{setfield}, except it omits the final @var{val} | 24 ## structure @var{s}. |
25 ## argument, returning this value instead of setting it. | |
26 ## | 25 ## |
27 ## @seealso{setfield, rmfield, isfield, fieldnames, isstruct, struct} | 26 ## If @var{s} is a structure array then @var{sidx} selects an element of the |
27 ## structure array, @var{field} specifies the field name of the selected | |
28 ## element, and @var{fidx} selects which element of the field (in the case of | |
29 ## an array or cell array). See @code{setfield} for a more complete | |
30 ## description of the syntax. | |
31 ## | |
32 ## @seealso{setfield, rmfield, orderfields, isfield, fieldnames, isstruct, struct} | |
28 ## @end deftypefn | 33 ## @end deftypefn |
29 | 34 |
30 ## Author: Etienne Grossmann <etienne@cs.uky.edu> | 35 ## Author: Etienne Grossmann <etienne@cs.uky.edu> |
31 | 36 |
32 function obj = getfield (s, varargin) | 37 function val = getfield (s, varargin) |
38 | |
33 if (nargin < 2) | 39 if (nargin < 2) |
34 print_usage (); | 40 print_usage (); |
35 endif | 41 endif |
42 | |
36 subs = varargin; | 43 subs = varargin; |
37 flds = cellfun ("isclass", subs, "char"); | 44 flds = cellfun ("isclass", subs, "char"); |
38 idxs = cellfun ("isclass", subs, "cell"); | 45 idxs = cellfun ("isclass", subs, "cell"); |
39 if (all (flds | idxs)) | 46 if (! all (flds | idxs)) |
40 typs = merge (flds, {"."}, {"()"}); | |
41 obj = subsref (s, struct ("type", typs, "subs", subs)); | |
42 else | |
43 error ("getfield: invalid index"); | 47 error ("getfield: invalid index"); |
44 endif | 48 endif |
49 | |
50 typs = merge (flds, {"."}, {"()"}); | |
51 val = subsref (s, struct ("type", typs, "subs", subs)); | |
52 | |
45 endfunction | 53 endfunction |
46 | 54 |
47 | 55 |
48 %!test | 56 %!test |
49 %! x.a = "hello"; | 57 %! x.a = "hello"; |
50 %! assert (getfield (x, "a"), "hello"); | 58 %! assert (getfield (x, "a"), "hello"); |
51 %!test | 59 %!test |
52 %! ss(1,2).fd(3).b = 5; | 60 %! ss(1,2).fd(3).b(1,4) = 5; |
53 %! assert (getfield (ss,{1,2},"fd",{3},"b"), 5); | 61 %! assert (getfield (ss,{1,2},"fd",{3},"b", {1,4}), 5); |
54 | 62 |
63 %% Test input validation | |
64 %!error getfield () | |
65 %!error getfield (1) | |
66 %!error <invalid index> getfield (1,2) | |
67 |