# How can I plot in real time where I have a circle be the current position, and I also have a line of every past position?

10 views (last 30 days)
Spencer Kraisler on 19 Jun 2019
Edited: Adam Danz on 24 Jun 2019
I'm plotting the trajectory of a quadrotor in a simulation I made. I have the 3D position of the quadrotor at every time step (from 0 to 100,000). Is there a way I can plot the quadrotors position in real time, in order to make a nice gif? I know hwo to do it with a single line, but I want a circle to represent the current positon of the quadrotor, but also a line of its past trajectory.

Adam Danz on 19 Jun 2019
Edited: Adam Danz on 24 Jun 2019
Option 1
Here's a demo to run that plots the progression of a curve as a line with a circle marker at the current coordinate. After producing the empty plot, it iteratively updates the XData and YData of each line object and refreshes the plot using drawnow(). You mentioned you had 100k data points so instead of updating on each iteration you may want to up date every 100 iterations or so after applying this to your real time data.
% Create fake data
time = 0:0.1:20;
data = sin(time);
% set up plot
figure();
lh = plot(nan, nan, 'b-');
hold on
mh = plot(NaN,NaN,'bo');
xlim([time(1),time(end)])
ylim([min(data),max(data)])
% Loop through data and update coordinates
for i = 1:numel(time)
lh.XData = time(1:i);
lh.YData = data(1:i);
mh.XData = time(i);
mh.YData = data(i);
drawnow();
end
Here's how to control how often the plot refreshes
iterationUpdate = 1 : 1000 : 100000; % every 1000 frames
% Loop through data and update coordinates
for i = 1:100000
...
if ismember(i,iterationUpdate)
drawnow();
end
end
Option 2
You can use a comet plot.
comet(time, data);

### Categories

Find more on Vector Fields in Help Center and File Exchange

R2019a

### Community Treasure Hunt

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

Start Hunting!