How to graph a moving plot while keeping another one static
Ältere Kommentare anzeigen
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 Kommentar
M S Zobaer
am 14 Mär. 2022
how to get "Symbolic Math Toolbox" for free?
thanks
Antworten (1)
Devineni Aslesha
am 22 Apr. 2020
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
Kategorien
Mehr zu 2-D and 3-D Plots finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!