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.
Deep copy of handle object?
71 次查看(过去 30 天)
显示 更早的评论
Does MATLAB seriously fail to provide a deep copy for a handle object (i.e. java's clone())?
Thanks in advance!
4 个评论
Walter Roberson
2020-9-10
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
2020-12-14
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.
回答(4 个)
Mark Mikofski
2012-12-17
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 个评论
Walter Roberson
2012-6-20
编辑:Walter Roberson
2020-9-10
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 个评论
Bill Tubbs
2022-6-5
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?
per isakson
2012-6-20
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 个评论
Walter Roberson
2019-10-30
-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.
James Tursa
2012-7-16
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).
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Custom Training Loops 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!