Matlab object assignment - copy an object instead of creating a pointer

26 Ansichten (letzte 30 Tage)
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 Kommentare
Guillaume
Guillaume am 17 Nov. 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
HiWave am 22 Aug. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Guillaume
Guillaume am 17 Nov. 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 Kommentare
Jan
Jan am 17 Nov. 2016
Bearbeitet: Jan am 17 Nov. 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
Guillaume am 17 Nov. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Variables finden Sie in Help Center und File Exchange

Tags

Noch keine Tags eingegeben.

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by