Issue with delete(obj)
1 次查看(过去 30 天)
显示 更早的评论
Hi, I am trying to create a path with an object that moves but then deletes the previous ones, I thought that using delete(obj) would delete it, but it just continues graphing all points. Anyone knows how I can solve this issue?
Here's the code:
clc; clear; close all;
px = 10;
py = 150;
j = 10;
c = 2;
dt = 0.01;
while j < 300
ruta = animatedline('Color','r','LineWidth',3,'LineStyle',':');
Vel = 150 + j;
V = min(50, Vel);
alpha = atand(150);
Vx = V*cosd(alpha);
Vy = V*sind(alpha);
px(c) = px(c-1)+Vx*dt;
py(c) = py(c-1)+Vy*dt;
j = px(c);
c = c+1;
addpoints(ruta,px,py);
auto = scatter(px,py,50,'filled');
drawnow;
delete(auto);
end
0 个评论
采纳的回答
Les Beckham
2023-11-29
Here are some modifications that should get you closer to what you wan't. Note the comments I've added in the code to point out the changes. Note that the animation won't show when run here in Answers, but it does work on the desktop.
The main issue was that you were overwriting the animated line plot with a scatter plot (unless I misunderstood what you were trying to do).
px = 10;
py = 150;
j = 10;
% c = 2; % don't need to index if you are adding one point at a time
dt = 0.01;
figure
ruta = animatedline('Color', 'r', 'LineWidth', 3, 'LineStyle', ':'); % <<< moved outside the loop
xlim([10 15]) % <<< specify axis limits so it doesn't keep resizing
ylim([0 1000])
grid on
while j < 15 % <<< reduced so this would run faster for testing
Vel = 150 + j;
V = min(50, Vel);
alpha = atand(150);
Vx = V * cosd(alpha);
Vy = V * sind(alpha);
px = px + Vx*dt; % you don't need to index if you are adding one point at a time
py = py + Vy*dt; % you don't need to index if you are adding one point at a time
j = px;
% c = c + 1; % you don't need to index if you are adding one point at a time
addpoints(ruta, px, py);
% auto = scatter(px, py, 50, 'filled'); % don't overwrite with the scatter plot
drawnow;
% delete(auto);
end
5 个评论
Les Beckham
2023-11-29
编辑:Les Beckham
2023-11-29
You are quite welcome. If your question is answered, please click Accept This Answer for the answer that helped you most. Thanks.
Also, if you are just getting started with Matlab, I would highly recommend that you take a couple of hours to go through the free online tutorial: Matlab Onramp
Note that the set() command replaces the x and y data of the existing "line" in the plot (which is actually just one point), so there is no need to delete anything.
更多回答(1 个)
Steven Lord
2023-11-29
Rather than creating and deleting a scatter plot over and over again, I think either using the comet function (if you have all the data before creating the plot) or updating the XData and YData property of the line created by a call to plot (if you're generating the points incrementally) would do what you want.
x = 0:360;
y = sind(x);
h = plot(NaN, NaN, 'o');
axis([0 360 -1 1]);
hold on
for k = 1:numel(x)
h.XData = x(k);
h.YData = y(k);
pause(1/60) % not too fast, not too slow
end
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!