plotting live data from a sensor

14 visualizzazioni (ultimi 30 giorni)
kelvin
kelvin il 6 Gen 2021
Risposto: dpb il 6 Gen 2021
Hello,
I'm trying to plot a line graph of the live data from my sensor. However, when I run my code is does not display the line.
Thanks in advance
code:
clear all;
S = serialport('COM5',115200);
temp =[];
accx=[];
accy=[];
accz=[];
i = 0;
while true
if S.NumBytesAvailable > 0
data = readline(S);
temp = [str2double(split(data,','))];
accx = [accx;temp(1)];
accy = [accy;temp(2)];
accz = [accz;temp(3)];
plot(i,temp(1))
i=i+1;
end
end

Risposta accettata

dpb
dpb il 6 Gen 2021
You are plotting only one point at a time and without having executed a hold on instruction, each call to plot creates a new line object, deleting the previous. So, at most you would have a dot moving across the axis if the marker size were large enough to see it. To observe this, change your present plot() call to
plot(i,temp(1),'markersize',10)
To do this kind of thing you need to use hold on and update the XData,YData arrays instead, or use the new AnimatedLine feature. See
for details and examples.

Più risposte (0)

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by