1 | function mut = mutable(v)
|
---|
2 | %MUTABLE Convert to a mutable object.
|
---|
3 | % mutable(v) returns a mutable copy of v.
|
---|
4 | % v can be a numeric array, cell array, or structure (but not a string or
|
---|
5 | % user-defined object).
|
---|
6 | % mutable (with no arguments) is equivalent to mutable(0).
|
---|
7 | %
|
---|
8 | % Mutable objects are special because if you change them, the changes are
|
---|
9 | % visible to all parts of the program. Ordinary Matlab values do not
|
---|
10 | % have this property; changes must be explicitly passed from one routine
|
---|
11 | % to another.
|
---|
12 | %
|
---|
13 | % A mutable object is accessed and modified via subscripted reference, such
|
---|
14 | % as x.a or x(5). Mutable structures support getfield, setfield, rmfield,
|
---|
15 | % isfield, and fieldnames.
|
---|
16 | %
|
---|
17 | % The result is mutable all the way down, e.g. if
|
---|
18 | % x = mutable(struct('a',[4 5]))
|
---|
19 | % then x.a and x.a(1) are mutable.
|
---|
20 | %
|
---|
21 | % Fields can be added to mutable structures but mutable arrays cannot be
|
---|
22 | % resized. Thus there is little point to mutable([]) or mutable({}).
|
---|
23 | %
|
---|
24 | % A mutable object cannot be used as an ordinary Matlab value, e.g. in
|
---|
25 | % matrix operations. Use 'immutable' to convert to a Matlab value.
|
---|
26 | %
|
---|
27 | % Mutable objects are less efficient than ordinary matlab values, so
|
---|
28 | % you should use them sparingly. Mutable structures are the most efficient,
|
---|
29 | % followed by cell arrays and then numeric arrays.
|
---|
30 | %
|
---|
31 | % Examples:
|
---|
32 | % x = mutable;
|
---|
33 | % y = x; % y is a reference, not a copy
|
---|
34 | % x(1) = 4;
|
---|
35 | % y(1) % prints 4
|
---|
36 | %
|
---|
37 | % x = mutable(struct);
|
---|
38 | % y = x; % y is a reference, not a copy
|
---|
39 | % x.a = 4; % add new field
|
---|
40 | % y.a % prints 4
|
---|
41 | %
|
---|
42 | % See also IMMUTABLE.
|
---|
43 |
|
---|
44 | % Written by Tom Minka
|
---|
45 | % (c) Microsoft Corporation. All rights reserved.
|
---|
46 |
|
---|
47 | if nargin < 1
|
---|
48 | v = 0;
|
---|
49 | end
|
---|
50 | % java object must be a collection
|
---|
51 | mut.obj = toJava(v,1);
|
---|
52 | % cache the class name, to save time
|
---|
53 | mut.cl = class(mut.obj);
|
---|
54 | mut = class(mut,'mutable');
|
---|