How to plot data point by point and erasing the last one?

44 views (last 30 days)
Hello, I need to plot my data point by point, but I also need just one point on the plot (when new point arrived the one before to disappear). That is my code everything works good I just want to fix that:
y=[1,3,2,3,3];
n=numel(y)
figure
xlim([0 2])
ylim([0 2])
grid on
hold on
for ii=1:n
if y(ii)<1.1
scatter(0.5,0.5),hold on
pause(1)
else if y(ii)<2.2
scatter(1.5,1.5),hold on
else
scatter(0.5,1.5)
pause(1)
end
end
end

Accepted Answer

MHN
MHN on 26 Apr 2016
Edited: MHN on 26 Apr 2016
I have done pretty much animated plot with Matlab, What you need is a little trick !!
y=[1,3,2,3,3];
n=numel(y)
figure
xlim([0 2])
ylim([0 2])
grid on
hold on
for ii=1:n
if y(ii)<1.1
scatter(0.5,0.5) ,hold on
pause(1)
scatter(0.5,0.5,'w') ,hold on
else if y(ii)<2.2
scatter(1.5,1.5) ,hold on
pause(1)
scatter(1.5,1.5, 'w') ,hold on
else
scatter(0.5,1.5)
pause(1)
scatter(0.5,1.5,'w')
end
end
end
  2 Comments
Deepika Deepika
Deepika Deepika on 26 Sep 2022
hello...I need to plot the moving point in such a way that when this moving point go to boundary of this simulation area then it will not reflect back but it will go outside the boundary and disappear and then at the same time from the opposite direction at same angle (at which the previous point was disappeared ) ,the new point will get appear.
Here is my code,It works good but still not got the desired output so please help me for getting the output as mentioned above.....
clear all;
clc;
% ................parameters........................
MAX_X= 100;
MAX_Y =100;
NODES = 50;
tim_sim=3;
alpha=0.99;
velocity =10;
direction =60;
mean_velocity=mean(randn(5),'all');
mean_direction=mean(randn(5),'all');
%....................scan all existing nodes..................
x_tmp1=[];
y_tmp1=[];
for nb=1:NODES
x= (0 + (MAX_X-0)*rand);
y= (0 + (MAX_Y-0)*rand);
%..............node position in every instant t...........
for t=1:tim_sim
%...calculate the new position by using the new_formula of Gauss-Markov...
x_tmp = x + velocity * cos(direction);
x_tmp1=[x_tmp1 x_tmp];
y_tmp = y + velocity * sin(direction);
y_tmp1=[y_tmp1 y_tmp];
normal_velocity=normrnd(0,1);
normal_direction=normrnd(0,1);
%..calculate the new velocity and new direction by using the newformula of
%Gauss-Markov..
velocity_tmp=(alpha*velocity+(1-alpha)*mean_velocity+sqrt(1-alpha*alpha)*normal_velocity);
direction_tmp=(alpha*direction+(1-alpha)*mean_direction+sqrt(1-alpha*alpha)*normal_direction);
% .....node bounces on the margins......
if (x_tmp1<0)
x_tmp1 = -1.*x_tmp1;
direction = pi-direction;
mean_direction = pi-mean_direction;
end
if (x_tmp1>MAX_X)
x_tmp1 = 2*MAX_X - x_tmp1;
direction = pi-direction;
mean_direction = pi-mean_direction;
end
if (y_tmp1<0)
y_tmp1 = -1.*y_tmp1;
direction = -direction;
mean_direction = -1*mean_direction;
end
if (y_tmp1>MAX_Y)
y_tmp1 = 2*MAX_Y - y_tmp1;
direction = -direction;
mean_direction = -1*mean_direction;
end
% plot(x_tmp1,y_tmp1,'color',[0.3 0.3 1])
end
%.......................Animation.......................
n=length(y_tmp1);
% plot(x_tmp1,y_tmp1,'*')
% axis([0 MAX_X 0 MAX_Y])
% pause(0.05)
for i=1:n
plot(x_tmp1(i),y_tmp1(i),'*')
axis([0 MAX_X 0 MAX_Y])
pause(0.05)
end
end
xlabel('X (meters)');
ylabel('Y (meters)');

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 26 Sep 2022
Instead of creating two new scatter plots each step (one to cover the previous point in white, the other to create the new point) just update the coordinates of the point.
x = 0:360;
y = sind(x);
h = plot(x(1), y(1), 'o');
axis([0 360 -1 1])
for k = 2:numel(x)
drawnow expose
h.XData = x(k);
h.YData = y(k);
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!