How to make multiple dots move in matlab graph simultaneously

5 views (last 30 days)
I want N numbers of dots to move in the matlab graph.When I keep N as 3 then dots move almost simultaneously. But when I increase value of N say to 100 then one dot moves one step then other dot moves one step and so on.After each dot moved first step,the same cycle repeates for the 2nd step and so on.It seems like motion of dots more like discrete motion and not simultaneous continuouse motion.
Also I want each dot present in its previous position to be removed every time it update its position and put the dot in its updated position. I understand that dots are not getting removed because i used the function ''hold on''.Since I am plotting for each dot i have to use hold on.
I am pretty stuck at this one. Can u suggest what change I can make in my matlab code. Below is my matlab code so far:
pmax=20 % total no of dots
x=zeros(pmax,1) %initialise the coordinates of each dots
y=zeros(pmax,1)
dt=0.5
lifespan=30 % life span : the total no of steps each dot has to move
for i =1:lifespan
for k = 1 : pmax
grid on;
hold on;
x(k,:)=x(k,:)+rand()*dt %rand(): the random value of velocity of each dot
y(k,:)=y(k,:)+rand()*dt
plot(x(k,:), y(k,:), 'ro-');
axis([-5 5 -5 5])
pause(0.05);
hold off;
end
end
fprintf('Done!\n');

Answers (1)

Alex Mcaulley
Alex Mcaulley on 22 May 2019
Changing each line object:
pmax=20 % total no of dots
x=0 %initialise the coordinates of each dots
y=0
dt=0.5
lifespan=30 % life span : the total no of steps each dot has to move
for i =1:lifespan
if i == 1
for k = 1 : pmax
grid on;
hold on;
plot(x, y, 'ro-');
axis([-5 5 -5 5])
pause(0.05);
hold off;
end
else
h = findobj(gca,'Type','line');
for k = 1 : pmax
prevX = get(h(k),'XData');
prevY = get(h(k),'YData');
set(h(k),'XData',prevX+rand*dt)
set(h(k),'YData',prevY+rand*dt)
pause(0.05);
end
end
end
fprintf('Done!\n');

Categories

Find more on Animation 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!