How do I connect points in a scatter plot with lines inside an infinite loop?

2 vues (au cours des 30 derniers jours)
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);

Réponse acceptée

Voss
Voss le 16 Juin 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 commentaires
Samantha Bartholomew
Samantha Bartholomew le 16 Juin 2022
You're absolutely right - I had hold on in my code, but a subplot command was messing it up. Thanks so much!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Performance dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by