How can I copy a figure that includes a colorbar to another figure
16 views (last 30 days)
I am trying to copy multiple figures to subplots in a new figure. It works for most figures, but it is giving errors for any figures that contain a colorbar or legend. What do I have to do to successfully copy all elements of every figure?
Below is a script that I use to test. The first and second figures copy ok. When the third figure, which includes a colorbar is included, I receive the error "Error using copyobj Invalid child handle argument". If I exclude the colorbar from the third figure, then the third figure is copied without error.
Somehow the handle to the colorbar is not working with the copyobj function; what should I do to fix this?
x1 = (1:10);
y1 = x1;
x2 = (10:100);
y2 = 5*x2.^2 - 7.*x2;
z = peaks(25);
title(' My First Figure')
title(' My Second Figure')
title(' My Third Figure')
colorbar % The copying fails if this is included. Comment this out and it works.
% Copy the three existing figures to subplots in a new figure
figHandles = get(0, 'Children');
figCount = length(figHandles);
figParent = figure;
rowCount = 2;
colCount = 2;
% create a new subplot in the new figure, and save it's handle
hSubplotDestFig = subplot(rowCount,colCount,i,'parent',figParent);
% copy all children of the original figure to the subplot that
% was just created
hAxesOrig = findobj(figHandles(i),'type','axes');
axesChildrenOrig = get(hAxesOrig,'children');