3.0

3.0 | 1 rating Rate this file 26 downloads (last 30 days) File Size: 4.95 KB File ID: #22965

Clone Handle Object - using MATLAB OOP

by Bobby Nedelkovski

 

12 Feb 2009 (Updated 28 Sep 2009)

Code covered by BSD License  

How to clone an object (deep copy) which inherits reference behaviour from the 'handle' class.

Download Now | Watch this File

File Information
Description

The motivation for this demo is that if you have an instance (say 'a') of a custom class that subclasses 'handle', then the command 'b = a;' will produce another reference 'b' to the same object as initially referenced by 'a', effectively creating a 'shallow' copy of the object.

The link to this demo contains a test class which contains the code necessary to produce a clone or 'deep' copy of its instance by way of a method and alternatively, through its constructor.

A test script is provided to demonstrate the cloning of instances of the test class.

Written by Sam Oliver & Bobby Nedelkovski
The MathWorks Australia Pty Ltd
Copyright 2009, The MathWorks, Inc.

MATLAB release MATLAB 7.9 (2009b)
Zip File Content  
HTML Files HOW TO CREATE A COPY OF A HANDLES OBJECT
Other Files copyHandleObj/demoScript.m,
copyHandleObj/TestClass.m,
license.txt
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (4)
25 Jun 2009 Volkmar Glauche

I also faced this problem, but for more complicated objects (using e.g. protected and dependent properties). It turned out that just looping over all properties did not do the trick. I had to check all kind of meta-properties to make sure the copyobj method didn't crash.

25 Jun 2009 Volkmar Glauche

This is the code I used for copyobj:
        function new = copyobj(obj)
            % This method acts as a copy constructor for all derived
            % classes.
            new = feval(class(obj)); % create new object of correct subclass.
            mobj = metaclass(obj);
            % Only copy properties which are
            % * not dependent or dependent and have a SetMethod
            % * not constant
            % * not abstract
            % * defined in this class or have public SetAccess - not
            % sure whether this restriction is necessary
            sel = find(cellfun(@(cProp)(~cProp.Constant && ...
                ~cProp.Abstract && ...
                (~cProp.Dependent || ...
                (cProp.Dependent && ...
                ~isempty(cProp.SetMethod))),mobj.Properties));
            for k = sel(:)'
                new.(mobj.Properties{k}.Name) = obj.(mobj.Properties{k}.Name);
            end
end

26 Jun 2009 Volkmar Glauche  
04 Aug 2009 Bobby Nedelkovski

This File Exchange item is now under review. WARNING: Neither the original method (leveraging ‘TestClass.m’ with the ‘copyobj’ method as a template for your custom class) nor using Volkmar’s suggestion (inherit the Volkmar’s proposed ‘copyobj’ method by subclassing ‘TestClass.m’) produces a truly ‘deep’ copy of a handle object. Instead, both methods yield ‘deep’ copies of public properties in the custom superclass hierarchy yet they produce ‘shallow’ copies of private and protected properties. This has to do with the depth in scope of private and protected properties to an instance…

Case: Values of private properties declared in any superclass or protected properties declared in any grandparent class or higher cannot be copied. Accessor methods for private and protected properties can be customised – for instance, getMyProperty(obj). If a private property is modified with a custom set method and since this custom method is not part of the property’s meta-data (i.e. “GetMethod = []” in its corresponding “meta.property” object), one cannot copy the value of the private property using simple assignment with Dynamic Expressions. Moreover, you cannot temporarily modify the property’s “GetMethod=getMyProperty(obj)” nor “GetAccess=Public” meta-data fields to assume necessary control to copy private properties.

I am currently investigating possibilities in producing a truly ‘deep’ copy of a handle object. Please feel free to email any suggestions to bobby.nedelkovski@mathworks.com.au.

Please login to add a comment or rating.
Updates
07 May 2009

This demo has been updated to use Dynamic Expressions when copying properties of an object.

28 Sep 2009

Added copyright protection to Description field and associated files . Verified this item works with MATLAB R2009b.

Tag Activity for this File
Tag Applied By Date/Time
clone Cristina McIntire 13 Feb 2009 14:06:50
object oriented programming Bobby Nedelkovski 13 Feb 2009 14:07:03
copy Bobby Nedelkovski 15 Feb 2009 16:22:48
object Bobby Nedelkovski 15 Feb 2009 16:22:48
clone Quoc Tran Dinh 19 Jun 2009 06:50:31
clone Bobby Nedelkovski 29 Sep 2009 12:27:24
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com