Copy of an handle object
6 views (last 30 days)
Show older comments
Hello,
I have some problem copy handle objcet.
How can I edit the example code in: https://www.mathworks.com/help/matlab/matlab_oop/custom-copy-behavior.html here a copy:
classdef HandleCopy < matlab.mixin.Copyable
properties
Prop1 % Shallow copy
Prop2 % Handle copy
end
methods (Access = protected)
function cp = copyElement(obj)
% Shallow copy object
cp = copyElement@matlab.mixin.Copyable(obj);
% Get handle from Prop2
hobj = obj.Prop2;
% Create default object
new_hobj = eval(class(hobj));
% Add public property values from orig object
HandleCopy.propValues(new_hobj,hobj);
% Assign the new object to property
cp.Prop2 = new_hobj;
end
end
methods (Static)
function propValues(newObj,orgObj)
pl = properties(orgObj);
for k = 1:length(pl)
if isprop(newObj,pl{k})
newObj.(pl{k}) = orgObj.(pl{k});
end
end
end
end
end
How can I edit the code above to get working with my own class? Here my code:
%handleClass.m
classdef handleClass < handle
properties
p1
end
methods
function obj = handleClass(in)
obj.p1 = in;
end
end
end
% handleClassHandlePropCopyable1.m
classdef handleClassHandlePropCopyable1 < matlab.mixin.Copyable
properties
hndProp handleClass
end
methods
function obj = handleClassHandlePropCopyable1(in)
obj.hndProp = handleClass(in);
end
end
methods (Access = protected)
function cp = copyElement(obj)
% Shallow copy object
cp = copyElement@matlab.mixin.Copyable(obj);
% Copy handle
% Get handle from hndProp
hobj = obj.hndProp;
% Create default object
eval(class(hobj));
% Add public property values from orig object
handleClassHandlePropCopyable1.propValues(new_hobj,hobj);
% Assign the new object to property
cp.hndProp = new_hobj;
end
end
methods (Static)
function propValues(newObj,orgObj)
pl = properties(orgObj);
for k = 1:length(pl)
if isprop(newObj,pl{k})
newObj.(pl{k}) = orgObj.(pl{k});
end
end
end
end
end
Best regards,
Alex
0 Comments
Answers (1)
Abhaya
on 7 Oct 2024
Hi Alessandro,
To copy the properties that contain handles, it is essential to create a new default object of ‘handleClass’ and then assign the property values from the current object to this new object.
In the code you provided, the line ‘eval(class(hobj))’ attempts to create a default object of class ‘handleClass’. However, an error is raised because there is no default constructor defined in ‘handleClass’. Without a default constructor, MATLAB cannot instantiate the object when no input arguments are provided.
Adding a default constructor will resolve this error by specifying how to initialize the object with default properties.
To modify the constructor of ‘handleClass’, please refer to the code given below.
function obj = handleClass(in)
if nargin==0
obj.p1=0;
else
obj.p1=in;
end
end
In the ‘handleClassHandlePropCopyable1’ class, you can create default object ‘new_hobj’ by using the constructor given above.
new_hobj = eval(class(hobj));
For more information, please refer to the MATLAB documentation given below.
Hope this solves your query.
0 Comments
See Also
Categories
Find more on Methods in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!