How to graph a moving plot while keeping another one static
2 次查看(过去 30 天)
显示 更早的评论
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/285857/image.png)
Expected Output:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/285858/image.gif)
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
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
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!