> I need to make a deep copy of a hande object. Is there any easy way to do
> this in R2009a?
When I had to do this I added this method to my class:
% Make a copy of a handle object.
function new = copy(this)
% Instantiate new object of the same class.
new = feval(class(this));
% Copy all non-hidden properties.
p = properties(this);
for i = 1:length(p)
new.(p{i}) = this.(p{i});
end
end
Then, when I want to make a copy of
a = myclass;
I use something like this:
b = copy(a); % which reads nicely as b = copy of a
or, equivalently,
b = a.copy;
If you need to copy hidden properties which don't show up in the result
of properties(this), you can use
p = fieldnames(struct(this));
instead.
--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.
Doug Schwarz <s...@sig.for.address.edu> wrote in message <see-CD8C71.1...@news.frontiernet.net>...
Best regards,
Till
The function returns:
??? Getting the 'property_protected' property of the '...' class is not allowed.
Do you have a suggestion to make a deep copy of a hande object which also copies these properties?
Regards,
Peter
Doug Schwarz <s...@sig.for.address.edu> wrote in message <see-CD8C71.1...@news.frontiernet.net>...
Rather than trying to convert the class object into a struct array and
getting its field names, use meta-classes.
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_oop/br8b90p.html
--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
I came up with a bit of a hack for doing this. It uses file-handling, and therefore is not so elegant, but it seems to work. Just add this method to whatever class you want to copy:
function new = copy(this)
save('temp.mat', 'this');
Foo = load('temp.mat');
new = Foo.this;
delete('temp.mat');
end
Example usage (assumes copy() is a method in class MyClass):
A = MyClass();
B = A.copy();
If modifying the class is an option, then have your clone method in your
class instantiate a new empty handle object and set the appropriate
properties. Remember, inside a class method, you have access to all the
properties of the object. If you don't want to hard-code the list of
properties, you could always make use of the meta.class object for your
object along with dynamic "field" names (or your class's GET and SET
methods, or whatever other property access system you've devised.)
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_oop/br4iuh2.html
--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com
Thanks Steve,
Summing up, I think adding this function to the base class does the trick. Any remakrs?
function newObj = clone(this)
% create an empty instance of the class
newObj = eval(class(this));
% copy non-dependent properties, by creating a meta object for the class of 'this'.
mo = eval(['?',class(this)]);
ndp = findobj([mo.Properties{:}],'Dependent',false);
for idx = 1:length(ndp)
newObj.(ndp(idx).Name) = this.(ndp(idx).Name);
end
Thanks,
Boaz
Thanks Steve,