Issue with delete(obj)
Show older comments
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
Accepted Answer
More Answers (1)
Steven Lord
on 29 Nov 2023
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
1 Comment
Valentina
on 29 Nov 2023
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

