for a handle class contains another handle object. I would do deep copy "recursively". (the following one might face the loop situation.)
classdef B < handle
properties
a % A obj
end
methods
function this = B()
this.a = A();
end
% class A should have the SIMILAR
% clonePublic function
function newObj = clonePublic(this)
props = properties(this);
newObj = B();
for i = 1: length(props)
tmp = this.(props{i});
if(isa(tmp,'handle'))
newObj.(props{i}) = tmp.clonePublic();
else
newObj.(props{i}) = tmp ;
end
end
end
end
end
As of R2011A Matlab provides some sort of means for cloning/copying handle objects. Inherit from the matlab.mixin.Copyable class.
It does seem to have a lot of restrictions, but has been good enough for my needs at the moment.
classdef A < handle
properties
b = containers.Map
end
end
please note that containers.Map is itself a handle class, which is not covered in the copyobj function from Volkmar Glauche. You could recursively call copyobj for handle properties, but then you're facing the next problem of private properties (Maps has private props), which cannot be copied as already mentioned.
In order to copy this class, the "ugly" solution from Barry Ridge is unfortunately the only thing which works in order to deep copy an instance of class A.
any news on this issue from Mathworks?
best regards
kusi
Thank you for your suggestions Barry and Holger. I recall encountering Holger's File Exchange posting some time ago and I was going to suggest this in response to Barry's feedback - this is a good workaround given the trade-offs.
Comment only