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

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.

 采纳的回答

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.

更多回答(1 个)

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 个评论

This does not work for me. I get error:
Error using matlab.ui.Figure/get
Invalid or deleted object.
Error in microctd_turb_20apr (line 653)
hc = get(hf(i),'children')
@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
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

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Graphics Object Properties 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by