From: boaz on
"Steven Lord" <slord(a)mathworks.com> wrote in message <hu9mt4$pkh$1(a)fred.mathworks.com>...
>
> "Barry Ridge" <nobody.home(a)sogetthis.com> wrote in message
> news:hu8b0c$b9e$1(a)fred.mathworks.com...
> > "Guillaume Perrault Archambault" <guigui.the.bulldozer(a)gmail.com> wrote in
> > message <h5f1pe$2ll$1(a)fred.mathworks.com>...
> >> I need to make a deep copy of a hande object. Is there any easy way to do
> >> this in R2009a?
> >
> > 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
> slord(a)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