Mercurial > hg > octave-nkf
annotate scripts/miscellaneous/setfield.m @ 20830:b65888ec820e draft default tip gccjit
dmalcom gcc jit import
author | Stefan Mahr <dac922@gmx.de> |
---|---|
date | Fri, 27 Feb 2015 16:59:36 +0100 |
parents | e9a0bd0b125c |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19794
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 -*- | |
19468
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
21 ## @deftypefn {Function File} {@var{sout} =} setfield (@var{s}, @var{field}, @var{val}) |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
22 ## @deftypefnx {Function File} {@var{sout} =} setfield (@var{s}, @var{sidx1}, @var{field1}, @var{fidx1}, @var{sidx2}, @var{field2}, @var{fidx2}, @dots{}, @var{val}) |
15615
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
23 ## |
19468
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
24 ## Return a @emph{copy} of the structure @var{s} with the field member |
19793
0e1f5a750d00
maint: Periodic merge of gui-release to default.
John W. Eaton <jwe@octave.org>
diff
changeset
|
25 ## @var{field} set to the value @var{val}. |
19468
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
26 ## |
16816
12005245b645
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
16772
diff
changeset
|
27 ## For example: |
5820 | 28 ## |
29 ## @example | |
30 ## @group | |
15615
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
31 ## @var{s} = struct (); |
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
32 ## @var{s} = setfield (@var{s}, "foo bar", 42); |
5820 | 33 ## @end group |
34 ## @end example | |
35 ## | |
15615
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
36 ## @noindent |
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
37 ## This is equivalent to |
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
38 ## |
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
39 ## @example |
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
40 ## @var{s}.("foo bar") = 42; |
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
41 ## @end example |
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
42 ## |
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
43 ## @noindent |
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
44 ## Note that ordinary structure syntax @code{@var{s}.foo bar = 42} cannot be |
19468
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
45 ## used here, as the field name is not a valid Octave identifier because of |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
46 ## the space character. Using arbitrary strings for field names is |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
47 ## incompatible with @sc{matlab}, and this usage will emit a warning if the |
20057
e9a0bd0b125c
Rename 'matlab-incompatible' warning to 'language-extension'.
Carnë Draug <carandraug@octave.org>
parents:
20038
diff
changeset
|
48 ## warning ID @code{Octave:language-extension} is enabled. |
19468
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
49 ## @xref{XREFwarning_ids}. |
15615
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
50 ## |
19468
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
51 ## With the second calling form, set a field of a structure array. The |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
52 ## input @var{sidx} selects an element of the structure array, @var{field} |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
53 ## specifies the field name of the selected element, and @var{fidx} selects |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
54 ## which element of the field (in the case of an array or cell array). |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
55 ## The @var{sidx}, @var{field}, and @var{fidx} inputs can be repeated to |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
56 ## address nested structure array elements. The structure array index and |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
57 ## field element index must be cell arrays while the field name must be a |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
58 ## string. |
15615
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
59 ## |
19468
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
60 ## For example: |
15615
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
61 ## |
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
62 ## @example |
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
63 ## @group |
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
64 ## @var{s} = struct ("baz", 42); |
19468
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
65 ## setfield (@var{s}, @{1@}, "foo", @{1@}, "bar", 54) |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
66 ## @result{} |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
67 ## ans = |
15615
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
68 ## scalar structure containing the fields: |
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
69 ## baz = 42 |
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
70 ## foo = |
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
71 ## scalar structure containing the fields: |
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
72 ## bar = 54 |
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
73 ## @end group |
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
74 ## @end example |
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
75 ## |
19794
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19793
diff
changeset
|
76 ## The example begins with an ordinary scalar structure to which a nested |
19468
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
77 ## scalar structure is added. In all cases, if the structure index @var{sidx} |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
78 ## is not specified it defaults to 1 (scalar structure). Thus, the example |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
79 ## above could be written more concisely as |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
80 ## @code{setfield (@var{s}, "foo", "bar", 54)} |
15615
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
81 ## |
19468
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
82 ## Finally, an example with nested structure arrays: |
15615
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
83 ## |
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
84 ## @example |
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
85 ## @group |
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
86 ## @var{sa}.foo = 1; |
19468
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
87 ## @var{sa} = setfield (@var{sa}, @{2@}, "bar", @{3@}, "baz", @{1, 4@}, 5); |
15615
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
88 ## @var{sa}(2).bar(3) |
19468
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
89 ## @result{} |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
90 ## ans = |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
91 ## scalar structure containing the fields: |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
92 ## baz = 0 0 0 5 |
15615
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
93 ## @end group |
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
94 ## @end example |
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
95 ## |
19468
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
96 ## Here @var{sa} is a structure array whose field at elements 1 and 2 is in |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
97 ## turn another structure array whose third element is a simple scalar |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
98 ## structure. The terminal scalar structure has a field which contains a |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
99 ## matrix value. |
15615
808e4f13e220
doc: Update struct documentation to match new indexing rules
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
100 ## |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9849
diff
changeset
|
101 ## Note that the same result as in the above example could be achieved by: |
5820 | 102 ## |
103 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
104 ## @group |
19468
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
105 ## @var{sa}.foo = 1; |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
106 ## @var{sa}(2).bar(3).baz(1,4) = 5 |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
107 ## @end group |
5820 | 108 ## @end example |
19468
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
109 ## @seealso{getfield, rmfield, orderfields, isfield, fieldnames, isstruct, struct} |
5820 | 110 ## @end deftypefn |
111 | |
112 ## Author: Etienne Grossmann <etienne@cs.uky.edu> | |
113 | |
19468
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
114 function sout = setfield (s, varargin) |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
115 |
9849
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
116 if (nargin < 3) |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
117 print_usage (); |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
118 endif |
19468
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
119 |
9849
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
120 subs = varargin(1:end-1); |
19468
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
121 val = varargin{end}; |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
122 flds = cellfun ("isclass", subs, "char"); |
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
123 idxs = cellfun ("isclass", subs, "cell"); |
19468
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
124 if (! all (flds | idxs)) |
9849
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
125 error ("setfield: invalid index"); |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
126 endif |
19468
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
127 |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
128 typs = ifelse (flds, {"."}, {"()"}); |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
129 sout = subsasgn (s, struct ("type", typs, "subs", subs), val); |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
130 |
5820 | 131 endfunction |
132 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
133 |
5820 | 134 %!test |
135 %! x.a = "hello"; | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
136 %! x = setfield (x, "b", "world"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
137 %! y = struct ("a", "hello", "b", "world"); |
19468
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
138 %! assert (x, y); |
5820 | 139 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
140 %! oo(1,1).f0 = 1; |
19468
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
141 %! oo = setfield (oo,{1,2},"fd",{3},"b", {1,4}, 6); |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
142 %! assert (oo(1,2).fd(3).b(1,4), 6); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
143 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
144 ## Test input validation |
19468
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
145 %!error setfield () |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
146 %!error setfield (1) |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
147 %!error setfield (1,2) |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
148 %!error <invalid index> setfield (1,2,3) |
1fa328da03ef
Overhaul getfield, setfield, orderfields m-files.
Rik <rik@octave.org>
parents:
18601
diff
changeset
|
149 |