MATLAB Answers

Deep copy of handle object?

175 views (last 30 days)
D G
D G on 20 Jun 2012
Commented: Walter Roberson on 30 Oct 2019
Does MATLAB seriously fail to provide a deep copy for a handle object (i.e. java's clone())?
Thanks in advance!

  1 Comment

D G
D G on 10 Jul 2012
I ended up saving a copy of the object to a mat file in the temporary directory, reloading, and then deleting the file. Works perfect.

Sign in to comment.

Answers (4)

Mark Mikofski
Mark Mikofski on 17 Dec 2012
This topic is covered in this MATLAB newsgroup post clone (deep copy) a handle object. Note comp.soft-sys.matlab (CSSM) is now mirrored on Google Groups.
It looks like this has been implemented in MATLAB at least as early as R2012a (and possibly earlier).
Subclass your object to matlab.mixin.Copyable [1] instead of handle and that will expose the copy method [2] which will perform shallow copies (no dependent or nested objects or recursive properties).
The methods can be overloaded and an example is given for a method to make deep copies.
References:

  0 Comments

Sign in to comment.


Walter Roberson
Walter Roberson on 20 Jun 2012
Hmmm, so I have a handle class which allocates resources out of an internal poll, and creates objects that have a reference ID and uses the resource. Okay, now deep copy it.
The ID gets copied, the resource pointer gets... ? Copied? Or does there hypothetically exist a clone() virtual method for each superclass that would get invoked so the class could allocate another resource object, updating the reference ID? If it is a plain copy then when one of the two objects is deleted, the class is going to deallocate the resource associated with the reference ID and then you have problems when you go to use or delete the second copy. If it is not a plain copy, then can you guarantee there are additional resources available to allocate, and the operation is meaningful?
For example suppose I have a handle class that references a serial port object out of a modem pool. If you copy the same serial port then you get conflicts with resource use between the two objects. If you allocate a new modem out of the pool of modems, what if no modems were available, and is allocating a second modem what you really want to have happen in this situation?
If you deep-copy a handle that references a memory-mapped file, then should the file be duplicated?
I'm not convinced that providing an automatic deep copy method is a good idea in a language in which resources can be represented as well as pure data.

  5 Comments

Show 2 older comments
Malcolm Lidierth
Malcolm Lidierth on 10 Jul 2012
Take a look at Bloch's Effective Java book to see what problems Java's clone presents.
D G
D G on 16 Jul 2012
Still would be useful to have for 90% of classes instead of worrying about the 10% of the time it could cause problems. At some point the programmer has to be responsible for how he uses his tools.

Sign in to comment.


per isakson
per isakson on 20 Jun 2012
It's an issue, which has been discussed in the newsgroup
% Subject: clone (deep copy) a handle object
% From: Peter O'Connor
% Date: 15 Dec, 2011 23:35:11
% Message: 11 of 12
and in the FEX
% See FEX contribution with comments:
% Clone Handle Object - using MATLAB OOP
% by Bobby Nedelkovski (The MathWorks)
% 12 Feb 2009 (Updated 28 Sep 2009)
.
I ended up using save( ...., '-v6' ) and load.

  2 Comments

Nikolaus Koopmann
Nikolaus Koopmann on 30 Oct 2019
why with '-v6'?
Walter Roberson
Walter Roberson on 30 Oct 2019
-v6 stored objects a different way, enough so that the objects would end up rebuilt.
However, MATLAB has added new object facilities since the 2012 timeframe, and storing objects into -v6 is now not always viable.

Sign in to comment.


James Tursa
James Tursa on 16 Jul 2012
You could try a mex function, but I have not investigated how the API routine mxDuplicateArray behaves with handle-derived class variables:
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
if( nlhs > 1 ) {
mexErrMsgTxt("Too many outputs.");
}
if( nrhs != 1 ) {
mexErrMsgTxt("Need exactly one input.");
}
plhs[0] = mxDuplicateArray(prhs[0]);
}
Place the above code in a file on the MATLAB path, e.g. deepcopy.c, make that directory your current directory, and then compile as follows:
mex deepcopy.c
If prompted, pick any C/C++ compiler that shows up in the list (e.g., the LCC compiler).

  0 Comments

Sign in to comment.

Tags