很多例子教程了,但是沒有subplot的例子。我用subplot畫了一個AM調製的顯示的動圖,並且保存。
主要是用到for的方法。代碼如下:
clear;
clf;
close all
t = linspace(0,3,200);
signal = sin(2*pi*0.5*t)*0.5+0.5;
carrier = sin(2*pi*5*t);
amWave = signal.*carrier;
h=figure
set(gcf,'Position',get(0,'ScreenSize'));
for i = 1:length(t)-2
subplot(3,1,1);
plot(t(i:i+1),signal(i:i+1),'b','LineWidth',2);
axis([0,3,-1,1])
title("\fontsize{14}原始信號")
hold on
drawnow
subplot(3,1,2);
plot(t(i:i+1),carrier(i:i+1),'r','LineWidth',2);
axis([0,3,-1,1])
title("\fontsize{14}載波")
hold on
drawnow
subplot(3,1,3)
plot(t(i:i+1),amWave(i:i+1),'r','LineWidth',2)
axis([0,3,-1,1])
hold on
plot(t(i:i+1),signal(i:i+1),'b','LineWidth',2);
title("\fontsize{14}調幅信號")
hold on
drawnow
f=getframe(gcf);
imind=frame2im(f);
[imind,cm] = rgb2ind(imind,256);
if i == 1
imwrite(imind,cm,'AmModulation.gif','GIF', 'Loopcount',inf,'DelayTime',0.1);
else
imwrite(imind,cm,'AmModulation.gif','GIF','WriteMode','append','DelayTime',0.1);
end
end