Plotting data coming from serial port with time on X-Axis

20 views (last 30 days)
Hello! Everyone
I want to plot the temperature data coming from serial port per second and time_stamp it in MATLAB, and display the time information on X-Axis.
The data coming from serial port is in string format and after receiving it, i converted it into double and plotted using MATLAB plot function. But i am not able to understand, that how can i time stamp the received data and mention that time on x-axis.
Currently i created a circular array of 100 values, which will be increased later to show more information. My program is as follow:
% Delete Opened Ports in MATLAB
delete(instrfind)
% Create a Serial Object
ser = serial('COM5', 'BaudRate',9600, 'Timeout',10);
DATA_SIZE = 100;
temperature = zeros(DATA_SIZE, 1);
% timestamp = zeros(DATA_SIZE, 1);
% timestamp = datetime(timestamp, timestamp, timestamp);
% for i = 1:DATA_SIZE
% timestamp(i) = datetime;
% end
index = 1;
% Open Communication Port
fopen(ser);
while true
temp = fscanf(ser);
if size(temp) == [0][0]
disp 'Timeout Occurs'
disp 'Check Connections'
break
else
if index < DATA_SIZE
index = index+1;
else
% Reset Subscript Index
index = 1;
end
temperature(index) = str2double(temp);
% Time Stamp Temperature Values
% timestamp(index) = datetime;
% plot(timestamp, temperature, 'LineWidth',2,'Color',[0,0,1.0])
plot(temperature, 'LineWidth',2,'Color',[0,0,1.0])
ylim([0,80])
xlabel('Time \rightarrow')
ylabel('Temperature (C)\rightarrow');
title('Real-Time Temperature Plot');
drawnow
end
end
fclose(ser);
I tried to plot the time value on X-Axis but failed to get the correct output. Please someone help me regarding this. Any other suggestion are also welcomed. Thanks in advance.

Answers (1)

Walter Roberson
Walter Roberson on 18 Mar 2017
If you are using R2013b or later then I recommend you use animatedline()
  3 Comments
Walter Roberson
Walter Roberson on 9 Sep 2023
Note: Using datatime for coordinates in animatedline() became possible as of R2023a. Before that you would need to convert the timestamp to numeric form, possibly using now() and using datetick('x')
You should keep in mind that especially when USB devices are involved, that the timestamp taken after the fscanf() is finished is not going to be the actual time that the measurement was taken. It takes time for a measurement to be converted to printable form and queued to be written on the source device; and time before the transmission happens (USB does not send immediately); and time in transmission; and time to be received by the destination hardware; and time for the hardware to wake up the operating system to pay attention to the data; and time for the operating system to make the data available to MATLAB.
You should also keep in mind that there are typically at least 3 different clocks involved in realtime measurements:
  • the time the measuring device thinks it is
  • the time the MATLAB host computer thinks it is
  • the "real" time... but "real" according to what authority?
It is surpringly difficult to synchronize clocks... and even when relativity differences would be immeasurable, clocks in any kind of device that you are likely to be able to afford are going to drift relative to each other, running faster or slower than "real" time or each other. The timekeeping crystals on an expensive CPU are probably going to be more precise and less variable than the timekeeping crystals on an arduino for example.
DATA_SIZE = 100;
% Delete Opened Ports in MATLAB
delete(instrfind)
% Create a Serial Object
ser = serial('COM5', 'BaudRate',9600, 'Timeout',10);
AL = animatedline('LineWidth', 2, 'Color', [0,0,1.0], 'MaximumNumPoints', DATA_SIZE);
ylim([0,80])
xlabel('Time \rightarrow')
ylabel('Temperature (C)\rightarrow');
title('Real-Time Temperature Plot');
% Open Communication Port
fopen(ser);
while true
temp = fscanf(ser);
% Time Stamp Temperature Values
T = datetime('now');
if isempty(temp)
disp 'Timeout Occurs'
disp 'Check Connections'
break
else
temperature = str2double(temp);
addpoints(T, temperature);
drawnow
end
end
fclose(ser);

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!