How do I connect points in a scatter plot with lines inside an infinite loop?
조회 수: 3 (최근 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);
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Performance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!