Clear Filters
Clear Filters

Copy of an handle object

9 views (last 30 days)
Alessandro Giovannini
Alessandro Giovannini on 2 Dec 2021
Answered: Abhaya on 7 Oct 2024 at 10:23
Hello,
I have some problem copy handle objcet.
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

Answers (1)

Abhaya
Abhaya on 7 Oct 2024 at 10:23
Hi Alessandro,
To copy the properties that contain handles, it is essential to create a new default object of ‘handleClassand 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 objectnew_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.

Categories

Find more on Methods in Help Center and File Exchange

Tags

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!