How do I connect points in a scatter plot with lines inside an infinite loop?
3 views (last 30 days)
Show older comments
The code I have is meant to run through an infinite while loop to collect and plot incoming data in real time.
Within the loop, it gathers speed and time values and plots them as individual points on a scatter plot. Because the program only has a point to plot each time it runs through the loop, the command plot(time, speed_mph, 'b-') doesn't work. Because the code is supposed to plot in real time, I can't just store the data and plot it outside of the loop.
Is there a way to connect the last plotted point and new plotted point with a line without clearing previous data from the plot?
The current code for plotting is roughly as shown below.
figure(1)
hold on
plot(time, speed_mph, 'b.')
set(gcf, 'Position', [0, 0, 1, 2/3] .* get(0, 'Screensize'));
set(gca, 'Xtick', time, 'XTickLabel', timestring);
0 Comments
Accepted Answer
Voss
on 16 Jun 2022
You can use animatedline to do this. See the example code below.
% create an animatedline
speed_line = animatedline('Color','b');
% initialize t
t = 0;
% infinite loop
while true
% get the new time and speed
new_time = t;
new_speed = 100*rand();
% add the new point to the line
addpoints(speed_line,new_time,new_speed);
% update the graphics
drawnow();
% increment t
t = t + 0.1;
end
4 Comments
More Answers (0)
See Also
Categories
Find more on Graphics Performance 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!