MATLAB画图之多个图以子图的形式合为一个图

解决问题:已经画好了多个figure,在不重复添加画图程序时,将多个图合为一个图。

      通常我们会将各个图单独画出来看效果,但是写文章过程中,经常为了排版等问题,需要将几个图分别以子图的形式合为一个图,这时候使用subplot,然后将各个图重新在子图中画出,但是这种方法,我们需要将这些的画图程序重新写一遍或者复制一遍,在这里我们使用一个创建子对象的方法实现,不需要再添加画图程序。

      MATLAB中的图像,实际上是一个对象集合,打开任意图像,输入gcf就显示当前图像的对象组成:

 

其中与图像内容相关的子对象为:Children和CurrentAxes。

每个坐标轴对象(CurrentAxis)又有自己的子对象:

 

 解决方法:

如果将两个图做为子图重绘到新的figure,即需要将其中的坐标轴对象导出,复制到新的figure的子图中。

程序如下:

clear;
clc;
close all;
t = 0:0.001:10;
y1 = sin(t);
y2 = cos(t);
 
figure(1);
plot(t,y1);
 
figure(2);
plot(t,y2);
 
fig(1) = get(figure(1), 'CurrentAxes');                                            
fig(2) = get(figure(2), 'CurrentAxes');
figure(3);
subplot(2,1,1);
axChildren = get(fig(1),'Children');
copyobj(axChildren, gca);
subplot(2,1,2);
axChildren = get(fig(2),'Children');
copyobj(axChildren, gca);

程序运行结果:

figure 1:

 

figure 2:

 

 figure 3:

 

posted @ 2020-03-21 21:45  pupilLZT  阅读(3711)  评论(0编辑  收藏  举报
/* 鼠标点击求赞文字特效 */