Mercurial > hg > octave-lyh
annotate scripts/miscellaneous/setfield.m @ 14459:a22a41ab6824
doc: Add Colin Macdonald and Mike Miller to contributors.in
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Tue, 13 Mar 2012 01:30:26 -0400 |
parents | f3d52523cde1 |
children | 808e4f13e220 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
12931
diff
changeset
|
1 ## Copyright (C) 2000-2012 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 -*- | |
6547 | 21 ## @deftypefn {Function File} {[@var{k1}, @dots{}, @var{v1}] =} setfield (@var{s}, @var{k1}, @var{v1}, @dots{}) |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9849
diff
changeset
|
22 ## Set a field member in a (nested) structure array. For example: |
5820 | 23 ## |
24 ## @example | |
25 ## @group | |
8507 | 26 ## oo(1,1).f0 = 1; |
27 ## oo = setfield (oo, @{1,2@}, "fd", @{3@}, "b", 6); | |
5820 | 28 ## oo(1,2).fd(3).b == 6 |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9849
diff
changeset
|
29 ## @result{} ans = 1 |
5820 | 30 ## @end group |
31 ## @end example | |
32 ## | |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9849
diff
changeset
|
33 ## Note that the same result as in the above example could be achieved by: |
5820 | 34 ## |
35 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
36 ## @group |
8507 | 37 ## i1 = @{1,2@}; i2 = "fd"; i3 = @{3@}; i4 = "b"; |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9849
diff
changeset
|
38 ## oo(i1@{:@}).(i2)(i3@{:@}).(i4) == 6 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
9849
diff
changeset
|
39 ## @result{} ans = 1 |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
40 ## @end group |
5820 | 41 ## @end example |
42 ## @seealso{getfield, rmfield, isfield, isstruct, fieldnames, struct} | |
43 ## @end deftypefn | |
44 | |
45 ## Author: Etienne Grossmann <etienne@cs.uky.edu> | |
46 | |
47 function obj = setfield (obj, varargin) | |
9849
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
48 if (nargin < 3) |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
49 print_usage (); |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
50 endif |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
51 subs = varargin(1:end-1); |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
52 rhs = 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
|
53 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
|
54 idxs = cellfun ("isclass", subs, "cell"); |
9849
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
55 if (all (flds | idxs)) |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
56 typs = merge (flds, {"."}, {"()"}); |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
57 obj = subsasgn (obj, struct ("type", typs, "subs", subs), rhs); |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
58 else |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
59 error ("setfield: invalid index"); |
87fd803c583b
rewrite getfield and setfield
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
60 endif |
5820 | 61 endfunction |
62 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
63 |
5820 | 64 %!test |
65 %! x.a = "hello"; | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
66 %! 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
|
67 %! y = struct ("a", "hello", "b", "world"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
68 %! assert (x,y); |
5820 | 69 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
70 %! oo(1,1).f0 = 1; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
71 %! oo = setfield (oo,{1,2},"fd",{3},"b", 6); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
72 %! assert (oo(1,2).fd(3).b, 6); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
73 |