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)
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.

Answers (1)

Adam Danz
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
lh = plot(nan, nan, 'b-');
hold on
mh = plot(NaN,NaN,'bo');
% 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);
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)
Option 2
You can use a comet plot.
comet(time, data);


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