How to graph a moving plot while keeping another one static

조회 수: 4 (최근 30일)
David Bustamante
David Bustamante 2020년 4월 19일
댓글: M S Zobaer 2022년 3월 14일
I have to write some code to graphically demonstrate the process of convolution. So far I managed create a shifting square wave that moves from left to right. However whenever I try to add a static square wave and do hold on, the same graph is written over and over again. I understand explaining this through words might be a little confusing so I'll just post screenshots of what I currently have and what I want to achieve as well as my code.
Current output:
Expected Output:
x = linspace(-5, 5, 1000);
ft = rect(x,3);
gt = rect(x,2);
pulse2 = [0 ones(1,200) 0];
figure
ax_1 = subplot(3,1,1);
plot(x,ft)
axis([min(x) max(x) 0 1.5])
title('f(t)')
ax_2 = subplot(3,1,2);
p = plot(x,ft);
hold(ax_2,'on')
title('Graphical Convolution')
for k = 1:numel(x)-numel(pulse2)
plot(x(k:k+numel(pulse2)-1), pulse2)
axis([min(x) max(x) 0 1.5])
drawnow
end
hold(ax_2,'off')

답변 (1개)

Devineni Aslesha
Devineni Aslesha 2020년 4월 22일
The graph overwriting can be avoided by updating the property 'XData' of the 'plot' function as follows.
pp = plot(x(1:1+numel(pulse2)-1), pulse2);
for k = 1:numel(x)-numel(pulse2)
pp.XData = (x(k:k+numel(pulse2)-1));
axis([min(x) max(x) 0 1.5])
drawnow
end

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by