How do I copy a figure object and its descendants to another object of the same class in MATLAB?

5 views (last 30 days)
I have a figure with several objects such as curves, annotations etc. I want to be able to copy all the objects in the figure to another figure while retaining the same hierarchy.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 8 Sep 2010
The function COMPCOPY below is an example of how to implement such a functionality.
function compCopy(op, np)
%COMPCOPY copies a figure object represented by "op" and its % descendants to another figure "np" preserving the same hierarchy.
ch = get(op, 'children');
if ~isempty(ch)
nh = copyobj(ch,np);
for k = 1:length(ch)
The function can be used as follows
hf1 = figure;
ax1 = subplot(211);
title('First Figure');
ax2 = subplot(212);
title('Second Figure');
hf2 = figure;
compcopy(hf1, hf2);
For information on the COPYOBJ function execute the following at the MATLAB command prompt:
doc copyobj

More Answers (0)


Find more on Graphics Object Identification in Help Center and File Exchange


No tags entered yet.


Community Treasure Hunt

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

Start Hunting!