MATLAB - Copy Objects



When working with objects in MATLAB, it is essential to understand how they behave during copy operations. MATLAB objects fall into two categories: handle objects and value objects. The distinction between these two types affects how copies of objects are managed and how changes to one copy impact others.

Value Objects behave like fundamental data types such as arrays or numeric values, where each copy is an independent entity. Changes made to one copy do not affect any other copies.

Handle Objects, on the other hand, operate through references. Copies of a handle object do not create new independent instances; instead, they reference the same underlying object. Thus, modifications made via one handle reference are visible through all other references to the same object.

Understanding these behaviors is crucial for effective programming in MATLAB, especially when managing complex data structures and ensuring that your code behaves as expected.

Let us understand the Value object in more detail.

Value Object

Value objects in MATLAB operate similarly to fundamental data types such as numbers and arrays. When you copy a value object, the copy is an entirely independent entity. Any modifications made to one copy do not impact any other copies of that object. This behavior ensures that each copy maintains its state, unaffected by changes to others.

For example, consider a value object A. If you create a copy B of A, both A and B will exist as separate instances. Any operations performed on B will not influence A, and vice versa.

Value Object Example

classdef ValueClass
    properties
        Property1
    end
end

obj1 = ValueClass;
obj1.Property1 = 10;
obj2 = obj1; % Create a copy of obj1

obj2.Property1 = 20; % Modify obj2

% obj1 remains unchanged
disp(obj1.Property1); % Output: 10
disp(obj2.Property1); % Output: 20

Handle Objects

Handle objects, on the other hand, function differently. A handle object is referenced through a handle variable, and copying this variable does not create a new, independent object. Instead, all copies of the handle variable point to the same underlying object. Consequently, any changes made through one handle variable are reflected across all other handle variables that reference the same object.

For instance, if H1 is a handle object and H2 is a copy of H1, both H1 and H2 refer to the same object. Therefore, an operation performed on H2 will be immediately visible when accessing the object through H1.

Handle Object Example

classdef HandleClass < handle
    properties
        Property1
    end
end

obj1 = HandleClass;
obj1.Property1 = 10;
obj2 = obj1; % Create a handle to obj1

obj2.Property1 = 20; % Modify obj2

% obj1 reflects the change
disp(obj1.Property1); % Output: 20
disp(obj2.Property1); % Output: 20

Copying Objects with copyobj in MATLAB

Use the copyobj function to copy objects from one parent to another. The new copy will differ from the original in several ways −

  • The Parent property will be set to the new parent.
  • The handle of the copied object will be different from the original.
  • copyobj does not copy the original objects callback properties.
  • Any application data associated with the original object is not copied.

As a result, comparing the original and new handles using == and isequal will return false.

You can copy various objects to a new parent or one object to multiple new parents, provided the parent/child relationship is correctly maintained. When copying an object with child objects, MATLAB will also copy all the children.

Syntax

new_handle = copyobj(h,p)

copyobj duplicates graphics objects and assigns them to a new parent object. The new parent must be suitable for the type of object being copied (for example, you can only copy axes to a figure or a panel). The function also copies any child objects along with the parent.

Syntax Explanation

new_handle = copyobj(h, p) copies one or more graphics objects specified by h and places them under the graphics object p. new_handle is the handle to the newly created object(s) or an array of handles for multiple copied objects.

When using copyobj, certain properties and objects that rely on their original context won't be copied over. For example, elements like legends and colorbars generate new context menus for the copied object, and new figures will have fresh toolbars and menus.

Heres what copyobj does not copy:

  • Callback Functions − Event-triggering functions wont transfer unless using an older method (legacy option).
  • Application Data − Any extra data linked to the object wont carry over unless using the legacy option.
  • Context Menus − Default context menus for legends, colorbars, and similar objects are not copied.
  • Toolbars and Menus − The default toolbar and menu options in figures are recreated rather than copied.
  • Special Axes − Axes used with the yyaxis function wont be copied.
  • Axes Interactions − Interactions associated with axes, like zoom and pan, arent copied.
  • DataTipTemplate − Custom data tip settings on objects like lines, scatter plots, and surfaces wont be transferred.

Also, you cant copy the same object more than once to the same parent in one copyobj call.

MATLAB automatically updates the Parent property to the new parent object and assigns new handles to the copied objects.

Example of using copyobj

The code we have is −

% Create a figure with some graphical objects
fig1 = figure('Name', 'Original Figure');
ax1 = axes('Parent', fig1);
plot(ax1, rand(10,1));

% Copy the figure to a new parent (another figure)
fig2 = figure('Name', 'Copied Figure');
newAx = copyobj(ax1, fig2);

% Update properties or callbacks as needed
set(newAx, 'Color', 'yellow');

In this example −

  • A figure (fig1) with an axis and plot is created.
  • The axis (ax1) is copied to a new figure (fig2) using copyobj.
  • The copied axis (newAx) is modified (e.g., changing the background color), while the original figure and axis remain unchanged.

On execution of the code in matlab command window the output we get is −

Advertisements