MATLAB Answers

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

80 views (last 30 days)
Arun Sharma
Arun Sharma on 18 Mar 2017
Edited: Arun Sharma on 18 Mar 2017
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()
  1 Comment
Arun Sharma
Arun Sharma on 18 Mar 2017
Thanks for your reply But I am sorry, as i didn't get that, how is this function going to solve my problem, of time stamping the received data and displaying on X-Axis, with timing information.
Yes i am using version greater than R2013b and will try this function, but to me it just looks to make animation of plotting and nothing related to time data on x-axis. Please correct me if i am wrong.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!