How can I plot an existing MATLAB plots all in one figure using SUBPLOT

7 views (last 30 days)
I have four figures which are already plotted by another user, but I do not have the commands that he used to plot them. I am trying to plot them all in one figure using SUBPLOT command.

Accepted Answer

Doug Hull
Doug Hull on 12 Jan 2011
One way is to obtain the ‘XData’ and ‘YData’ of all the children of the figures then plot them again in one figure using the SUBPLOT command.
The following example shows how to obtain the ‘XData’ and ‘YData’:
plot(1:10) % for example this is your existing figure
ha1 = get(gca,'Children') %this returns the children of the axes
a1 = get(ha1,'Xdata') % obtain the XData
b1 = get(ha1,'Ydata') % obtain the YData
figure; % new figure
plot(a1,b1) %plot the data again
Similarly, obtain the XData and YData of all other figures then use SUBPLOT to plot them in one figure.

More Answers (1)

Doug Hull
Doug Hull on 12 Jan 2011
A more generic way to do this is to get the handles of the children of the axes you want to copy and reparent them. This will not cary over view information, but will get the information over. Works well for plots.
hf(1) = figure(1)
plot(peaks);
hf(2) = figure(2)
plot(membrane)
hf(3) = figure(3)
ha(1) = subplot(1,2,1);
ha(2) = subplot(1,2,2);
for i = 1:2
hc = get(hf(i),'children')
hgc = get(hc, 'children');
set(hgc, 'parent',ha(i));
end
  3 Comments
Bruno Luong
Bruno Luong on 3 Aug 2023
@Francesca Pisani this code runs fine on online server as showed here:
Do not close manually he figure when it still running.
hf(1) = figure(1)
hf =
Figure (1) with properties: Number: 1 Name: '' Color: [1 1 1] Position: [671 558 577 433] Units: 'pixels' Show all properties
plot(peaks);
hf(2) = figure(2)
hf =
1×2 Figure array: Figure Figure
plot(membrane)
hf(3) = figure(3)
hf =
1×3 Figure array: Figure Figure Figure
ha(1) = subplot(1,2,1);
ha(2) = subplot(1,2,2);
for i = 1:2
hc = get(hf(i),'children')
hgc = get(hc, 'children');
set(hgc, 'parent',ha(i));
end
hc =
Axes with properties: XLim: [0 50] YLim: [-8 10] XScale: 'linear' YScale: 'linear' GridLineStyle: '-' Position: [0.1300 0.1100 0.7750 0.8150] Units: 'normalized' Show all properties
hc =
Axes with properties: XLim: [0 35] YLim: [-0.4000 1] XScale: 'linear' YScale: 'linear' GridLineStyle: '-' Position: [0.1300 0.1100 0.7750 0.8150] Units: 'normalized' Show all properties
Francesca Pisani
Francesca Pisani on 3 Aug 2023
Thank you, here it works, but if I apply this code to my need of creating a subplots with 4x2 figures it doesn't work. Please, if you could help me, I've just uploaded my question here https://it.mathworks.com/matlabcentral/answers/2004122-how-to-get-subplots-made-of-a-group-of-already-existing-fig

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!