MATLAB Answers

Deep copy of handle object?

153 views (last 30 days)
D G on 20 Jun 2012
Commented: Ben Barnett on 14 Dec 2020
Does MATLAB seriously fail to provide a deep copy for a handle object (i.e. java's clone())?
Thanks in advance!
Ben Barnett
Ben Barnett on 14 Dec 2020
If you are using the save-to-file-then-reload workaround. You might want to try using the undocumented Matlab functions described here.
copyStream = getByteStreamFromArray(objToCopy);
objCopy = getArrayFromByteStream(copyStream);
This should remove the file IO access from the process.

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.

Walter Roberson
Walter Roberson on 20 Jun 2012
Edited: Walter Roberson on 10 Sep 2020
Hmmm, so I have a handle class which allocates resources out of an internal pool, 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.
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.
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).
  1 Comment
James Tursa
James Tursa on 10 Sep 2020
Checked ... and this doesn't make a deep copy.

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!

Translated by