Deep copy of handle object?

71 vues (au cours des 30 derniers jours)
D G
D G le 20 Juin 2012
Commenté : Bill Tubbs le 5 Juin 2022
Does MATLAB seriously fail to provide a deep copy for a handle object (i.e. java's clone())?
Thanks in advance!
  4 commentaires
Walter Roberson
Walter Roberson le 10 Sep 2020
I don't think it can work until you can solve the issues discussed in https://www.artima.com/intv/issues.html#part3
Ben Barnett
Ben Barnett le 14 Déc 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.

Connectez-vous pour commenter.

Réponses (4)

Mark Mikofski
Mark Mikofski le 17 Déc 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:

Walter Roberson
Walter Roberson le 20 Juin 2012
Modifié(e) : Walter Roberson le 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.
  6 commentaires
D G
D G le 16 Juil 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.
Bill Tubbs
Bill Tubbs le 5 Juin 2022
This discussion seems quite old so not sure if it is still relevant. Found this documentation page:
Is this what we should be using now?

Connectez-vous pour commenter.


per isakson
per isakson le 20 Juin 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 commentaires
Nikolaus Koopmann
Nikolaus Koopmann le 30 Oct 2019
why with '-v6'?
Walter Roberson
Walter Roberson le 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.

Connectez-vous pour commenter.


James Tursa
James Tursa le 16 Juil 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 commentaire
James Tursa
James Tursa le 10 Sep 2020
Checked ... and this doesn't make a deep copy.

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by