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

조회 수: 27 (최근 30일)
Ryan Sinfield
Ryan Sinfield 2016년 11월 17일
댓글: HiWave 2020년 8월 22일
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
Guillaume
Guillaume 2016년 11월 17일
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 2020년 8월 22일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2016년 11월 17일
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
Jan
Jan 2016년 11월 17일
편집: Jan 2016년 11월 17일
@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 2016년 11월 17일
@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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by