serial data & real time plot

1 次查看(过去 30 天)
vishwash saxena
vishwash saxena 2011-3-6
i want to get a serial data and plot its value against the time...
here is the code
obj = serial('COM1') %creating the object
set(obj,'BaudRate',9600) %setting baudrate
fopen(obj); %open port
set(obj,'terminator','cr') %providing the terminator
a1=0; % a variable for y axis
time1=now; % time is in x axis
while 2>1 % infinite loop
time2=now;
x=[time1 time2];
ip_data = fscanf(obj) ;
a2= str2num(ip_data) ;
a=[a1 a2];
line(x,a);
datetick('x','HH:MM') %change the axis
pause(0.5);
a1=a2;
time1=time2;
end
is it correct???

回答(1 个)

Paulo Silva
Paulo Silva 2011-3-6
while 2>1 % infinite loop
you can do it better
while 1 %same as while 2>1
Now for the plotting, first create a line (before the loop) with NaN values, liH=line(NaN,NaN) and inside the loop set the lines values.
set(liH,'XData',[get(liH,'XData') x]);
set(liH,'YData',[get(liH,'YData') a]);
If it gets too slow please consider the preallocation of XData and YData, fill it with NaN values and when the loop gets to the end of the preallocated data do something to "reset" it, if your script only runs for a short while you don't have to do it.

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by