Discussion:
copy existing plot to subplot
(too old to reply)
Jason
2007-12-09 17:36:55 UTC
Permalink
Hi,

I am trying to find a way to copy plots from existing
figures into subplots in a new figure. Does anyone know of a
way to do this?

Thanks!
-Jason
Jason
2007-12-09 17:50:54 UTC
Permalink
Post by Jason
Hi,
I am trying to find a way to copy plots from existing
figures into subplots in a new figure. Does anyone know of a
way to do this?
Thanks!
-Jason
P.S. I want the axis labels, titles, legends, etc. to get
placed on the subplot as well.

Thanks again,
-Jason
Bruno Luong
2007-12-09 17:52:11 UTC
Permalink
help copyobj

Bruno
Jason
2007-12-09 20:19:03 UTC
Permalink
Post by Bruno Luong
help copyobj
Bruno
Thanks, Bruno-

I tried

figure(1);
h1=plot(rand(100,1));grid
title('some title'),xlabel('time');ylabel('amplitude')
figure(2);
s1=subplot(211);
copyobj(h1,s1)

but the title and labels do not end up on the subplot.

Using:

copyobj(get(h1,'parent'),get(s1,'parent'))

brings the title, and labels along with the plot (as well as
the grid), but it makes a new plot on top of the subplot
(not quite what I wanted); finally

copyobj(get(h1,'parent'),s1)

is not a valid command.

So, is it necessary to do some sort of loop to get the
title, labels and grid settings from the plot copied to the
subplot?

Thanks-
-Jason
Bruno Luong
2007-12-09 20:53:38 UTC
Permalink
Jason,

This might be better:

figure(1);
ax1=axes('Parent',1);
plot(ax1,rand(100,1));
title(ax1,'some title');
xlabel(ax1,'time');
ylabel(ax1,'amplitude');

figure(2);
ax2=subplot(211);
copyobj(allchild(ax1),ax2);

Bruno
Jason
2007-12-09 22:34:27 UTC
Permalink
Post by Bruno Luong
Jason,
figure(1);
ax1=axes('Parent',1);
plot(ax1,rand(100,1));
title(ax1,'some title');
xlabel(ax1,'time');
ylabel(ax1,'amplitude');
figure(2);
ax2=subplot(211);
copyobj(allchild(ax1),ax2);
Bruno
Bruno-

"allchild" is very cool, is nearly exactly what I need, and
it works great for the snippet above.

Please let me apologize since I now see that I should have
explained the over all application.

Say you have a series of fig files and want to open and go
through them one at a time to select a number of them for
inclusion on a new series of subplots. The idea is that you
could open the ones that you want and then somehow execute
all the necessary "copyobj" commands to get them transferred
to subplots. Ostensibly, this would even be done when the
source fig files had multiple curves and possibly legends
along with the title and label text items (but the source
figs would not themselves contain subplots).

Is this application in danger of becoming a big
research/development task? I've been playing around with
permutations of "copyobj", "allchild", and "get" commands
and am not really getting close to something that works.

Thanks,
-Jason
JWagberg
2007-12-10 00:56:25 UTC
Permalink
Post by Jason
Post by Bruno Luong
Jason,
figure(1);
ax1=axes('Parent',1);
plot(ax1,rand(100,1));
title(ax1,'some title');
xlabel(ax1,'time');
ylabel(ax1,'amplitude');
figure(2);
ax2=subplot(211);
copyobj(allchild(ax1),ax2);
Bruno
Bruno-
"allchild" is very cool, is nearly exactly what I need, and
it works great for the snippet above.
Please let me apologize since I now see that I should have
explained the over all application.
Say you have a series of fig files and want to open and go
through them one at a time to select a number of them for
inclusion on a new series of subplots. The idea is that you
could open the ones that you want and then somehow execute
all the necessary "copyobj" commands to get them transferred
to subplots. Ostensibly, this would even be done when the
source fig files had multiple curves and possibly legends
along with the title and label text items (but the source
figs would not themselves contain subplots).
Is this application in danger of becoming a big
research/development task? I've been playing around with
permutations of "copyobj", "allchild", and "get" commands
and am not really getting close to something that works.
Thanks,
-Jason
figure(1);
plot(rand(100,1));grid
title('some title'),xlabel('time');ylabel('amplitude')
hax1=gca;
hf2=figure(2);
s1=subplot(211);
pos=get(s1,'Position');
delete(s1);
hax2=copyobj(hax1,hf2);
set(hax2, 'Position', pos);

HTH,
jewa & wagberg , net (replace the obvious)
Bruno Luong
2007-12-10 07:29:26 UTC
Permalink
Post by Jason
Is this application in danger of becoming a big
research/development task? I've been playing around with
permutations of "copyobj", "allchild", and "get" commands
and am not really getting close to something that works.
You need to pay attention to legend object, which is and
axis with Tag set to 'legend'. Legend is a child of figure
and not the axis, which is an obstacle for copying. You
could use logical flag '-not', '-and' to findobj or find all
to separate real axes object and legend object.

Bruno
JWagberg
2007-12-10 07:51:54 UTC
Permalink
Post by Bruno Luong
Post by Jason
Is this application in danger of becoming a big
research/development task? I've been playing around with
permutations of "copyobj", "allchild", and "get" commands
and am not really getting close to something that works.
You need to pay attention to legend object, which is and
axis with Tag set to 'legend'. Legend is a child of figure
and not the axis, which is an obstacle for copying. You
could use logical flag '-not', '-and' to findobj or find all
to separate real axes object and legend object.
Bruno
Ouch, your right, Bruno. I missed that legend when submitted my little
fix.

jewa
Jason
2007-12-10 15:42:45 UTC
Permalink
Post by JWagberg
Post by Bruno Luong
Post by Jason
Is this application in danger of becoming a big
research/development task? I've been playing around with
permutations of "copyobj", "allchild", and "get" commands
and am not really getting close to something that works.
You need to pay attention to legend object, which is and
axis with Tag set to 'legend'. Legend is a child of figure
and not the axis, which is an obstacle for copying. You
could use logical flag '-not', '-and' to findobj or find all
to separate real axes object and legend object.
Bruno
Ouch, your right, Bruno. I missed that legend when
submitted my little
Post by JWagberg
fix.
jewa
Thanks for the code, Jewa. That seems to work really well.
I'll need to play around with it a little more to figure
out how to grab the legend (when one exists) from the source
figure, but that gets me 95% of the way there.

It looks like once I get a handle (call it hl) to the
'legend' type axis from the source figure doing something like

copyobj(lh,get(gca,'parent'))

works as long as gca is the target subplot-- it will
probably also require doing a "dummy" legend, getting its
position and then deleting it so that the copied legend can
be moved to the desired position (like you did to get the
subplot).

Thanks again everyone!
-Jason
Peter O'Connor
2011-04-08 10:56:04 UTC
Permalink
Here's a semi-complete function that seems to work ok except for the legend. It takes care of the problem of certain text things being put into funny positions when copying onto a smaller subplot.

function ax2ax(hSource,hDest)
% Copy everything on one axes to another.
% Still doesn't do legend. Don't know abouut colorbars, but hey its a
% start.

% Find children, copy non-text ones.
kids=allchild(hSource);
nontextkids=kids(~strcmp(get(kids,'type'),'text'));
copyobj(nontextkids,hDest);

% Axes Directions (may need to add other properties)
meth={'YDir','XLim','YLim'};
cellfun(@(m)set(hDest,m,get(hSource,m)),meth);

% Special treatment for text-children
subplot(hDest)
xlabel(get(get(hSource,'xlabel'),'string'));
ylabel(get(get(hSource,'ylabel'),'string'));
title(get(get(hSource,'title'),'string'));

end
Yao e
2013-03-29 07:47:10 UTC
Permalink
Post by Peter O'Connor
Here's a semi-complete function that seems to work ok except for the legend. It takes care of the problem of certain text things being put into funny positions when copying onto a smaller subplot.
function ax2ax(hSource,hDest)
% Copy everything on one axes to another.
% Still doesn't do legend. Don't know abouut colorbars, but hey its a
% start.
% Find children, copy non-text ones.
kids=allchild(hSource);
nontextkids=kids(~strcmp(get(kids,'type'),'text'));
copyobj(nontextkids,hDest);
% Axes Directions (may need to add other properties)
meth={'YDir','XLim','YLim'};
% Special treatment for text-children
subplot(hDest)
xlabel(get(get(hSource,'xlabel'),'string'));
ylabel(get(get(hSource,'ylabel'),'string'));
title(get(get(hSource,'title'),'string'));
end
For legend. suppose the GUI has 6 axes
for i=1:1:6
legend_pre=get(legend(eval(strcat('handles.axes',num2str(i)))),'String');
if~isempty(legend_pre)
legend(legend_pre);
else
end
end
Jason
2014-04-14 14:32:08 UTC
Permalink
Post by Jason
Post by Bruno Luong
Jason,
figure(1);
ax1=axes('Parent',1);
plot(ax1,rand(100,1));
title(ax1,'some title');
xlabel(ax1,'time');
ylabel(ax1,'amplitude');
figure(2);
ax2=subplot(211);
copyobj(allchild(ax1),ax2);
Bruno
Bruno-
"allchild" is very cool, is nearly exactly what I need, and
it works great for the snippet above.
Please let me apologize since I now see that I should have
explained the over all application.
Say you have a series of fig files and want to open and go
through them one at a time to select a number of them for
inclusion on a new series of subplots. The idea is that you
could open the ones that you want and then somehow execute
all the necessary "copyobj" commands to get them transferred
to subplots.
If I'm interpreting the above correctly, try the findobj command and use the other functions on the resulting handles:
figHandles = findobj('Type','figure');


-JW
Nedialko
2014-11-25 20:54:06 UTC
Permalink
In my opinion this is an unsolved essential issue.

Matlab has become a prime publishing tool and it should offer straightforward figure 'reflow' capacity - instead of all of us trying to 'patch' this on a case-by-case basis.

Anything you've seen in file-exchange that could point in the right direction?
Farhad Sedaghati
2015-06-22 14:32:03 UTC
Permalink
Post by Jason
Hi,
I am trying to find a way to copy plots from existing
figures into subplots in a new figure. Does anyone know of a
way to do this?
Thanks!
-Jason
You can use the following function:
http://www.mathworks.com/matlabcentral/fileexchange/51236-subplot
Continue reading on narkive:
Loading...