Matlab object assignment - copy an object instead of creating a pointer
19 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ryan Sinfield
el 17 de Nov. de 2016
Comentada: HiWave
el 22 de Ag. de 2020
Hi,
If I had an object variable and then assigned the same object to another variable, the latter acts as a pointer to the memory address of the original object instead of creating a copy of the original.
a = audioplayer(y, fs);
b = a;
set(b, 'SampleRate') = get(a, 'SampleRate') * 2;
play(a);
play(b);
In this example, a and b both have the same sample rate after the code is run. Is there any way to copy an entire object into a new variable instead of using a pointer to the memory address of the original object?
5 comentarios
Guillaume
el 17 de Nov. de 2016
The problem is:
a = instanceofhandleclass
a.prop1 = somevalue;
a.prop2 = someothervalue;
%... and so on, configure all properties of a
%now we want another object that is identical to a but for one property:
b = a; %not a copy due to shared memory
b.prop2 = somedifferentvalue; %also changes a.prop2!
If a is a value class (the default) then the b.prop2 = ... would trigger copy-on-write.
HiWave
el 22 de Ag. de 2020
I second this....I have a structure of 20 classes I want to make a copy of to save the state before making changes. I can't do that unless I save a .mat file then load it later.
Respuesta aceptada
Guillaume
el 17 de Nov. de 2016
Actually, the default in matlab is that assignment creates a copy. Only classes that are explicitly defined as handle classes exhibit pointer behaviour. The audioplayer class is indeed a handle class, and so it has pointer behaviour.
Unfortunately for you, unless the class is customised to override the default copy behaviour there is no built-in way of copying the object. audioplayer is not customised, so you have to copy each property yourself. This can be automated thankfully:
function b = copyobj(a)
b = eval(class(a)); %create default object of the same class as a. one valid use of eval
for p = properties(a).' %copy all public properties
try %may fail if property is read-only
b.(p) = a.(p);
catch
warning('failed to copy property: %s', p);
end
end
end
The above could be refined to only copy read/write properties.
3 comentarios
Jan
el 17 de Nov. de 2016
Editada: Jan
el 17 de Nov. de 2016
@Guillaume: Actually Matlab does not create a copy even on assignments on standard numerical arrays.
a = zeros(1, 1e6)
b = a
This does not duplicate the values, but creates a "shared data copy". The duplications happens only, if the contents of b is modified later.
In case of a copy of an audioplayer object, the object is "copied", but the signal is a pointer to the same memory. But why does the OP want to avoid this? What is the purpose of not using this copy-on-write method?
Even the "b.(p) = a.(p)" in this answer will create a shared-data-copy, most likely. But this is fine and saves memory. I do not see any drawback in this.
Guillaume
el 17 de Nov. de 2016
@Jan, yes, I know, matlab is copy-on-write. That's just an implementation detail, from the user's perspective every variable is a copy of the original variable and modifications are not reflected...
... except when the variable is an instance of a handle class. In which case, copy-on-write does not apply at all. This is what causes issue to Ryan since any modification to the copy is also reflected in the original
a = instanceofhandleclass;
a.someprop = 5;
b = a; %not a copy, so any change to b also changes a
b.someprop = 7; %also sets a.someprop to 5 since a and b are the same object
So, if you want two audioplayers objects that are identical but for a single property, you can just make a copy and change the property in the duplicate as that also changes the property of the original.
Más respuestas (0)
Ver también
Categorías
Más información sobre Variables en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!