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

6 次查看(过去 30 天)
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);

采纳的回答

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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by