MATLAB Answers

Arun Sharma

Plotting Temperature with respect to time in MATLAB from a file??

Asked by Arun Sharma
on 7 Oct 2013
Latest activity Commented on by Arun Sharma
on 10 Oct 2013

Hello!!! Everyone,

I am making a Data Logger Project and had stored Log in Memory Card, and wants to plot the Temperature Data with Respect to time in MATLAB.

18:30:04-> Time
05/10/13-> Date
025.7C -> Unit Temperature
+024.8C -> Atmosphere Temperature
058.7% -> Relative Humidity

Rest are Not useful

Can any one tell how to plot Temperature w.r.t time for each day on Separate graphs(Figure in MATLAB)

Please Help


Log in to comment.



No products are associated with this question.

2 Answers

Answer by Walter Roberson
on 7 Oct 2013
 Accepted answer

fid - fopen('YourFile.txt');
datacell = textscan(fid, '%s%s%fC%fC%f%*[^\n]', 'Delimiter', ',');
fulldates = strcat(datacell{2}, {' '}, datacell{1});
datenumbers = datenum(fulldates, 'mm/dd/yy HH:MM:SS');
unittemps = datacell{3};
atmtemps = datacell{4};
relhums = datecell{5};
fig1 = figure();
ax1 = axes('Parent', fig1);
plot(datenumbers, unittemps, 'Parent', ax1);
datetick(ax1, 'x', 'HH:MM:SS');
title(ax1, 'Time vs Unit Temperature, Uni');

The refinement after this would be to split it down to individual days. For example as a quick hack:

datechars = char(datacell{2});
differentdates = any(diff(datchars, 1), 2);

Then differentdates will be true at location K if the K'th date differs from the (K+1)'th date and so indicates that you should split after the K'th row. Note that the result will be one element shorter than the number of row entries.


Log in to comment.

Answer by Arun Sharma
on 7 Oct 2013
Edited by Walter Roberson
on 7 Oct 2013

I had attached the file, and its not working

I found some errors like

fid = fopen('YourFile.txt');
datacell = textscan(fid, '%s%s%fC%fC%f%*[^\n]', 'Delimiter', ',');

I will try to use your code.

Can i use datetick function in MATLAB for solving my problem


You are going to need to change the 'YourFile.txt' to 'LOGGER.CSV' .

datetick will not solve your problem by itself. Your problem has a couple of pieces:

  1. reading in the text and throwing away the unneeded parts
  2. converting the text dates into numeric dates
  3. breaking the input lines up by date
  4. plotting each day with its corresponding data on separate figure per day
  5. labeling the x axis of the plots with the time information

Of these tasks, datetick() only accomplishes the last, the labeling of the axis.

Thanks Sir, Actually i solved my Problem using dataticks, i will see your method to,it also looks interesting.

Thank You So Much, I tried your method and it works. :-)

Here is the Code i wrote.

fid = fopen('LOGGER.CSV','r');
DataCell = textscan(fid,'%s %s %fC %fC %f%*[^\n]',1329,'Delimiter',',')
FullDate = strcat(DataCell{2},{' '},DataCell{1})
DateNumbers = datenum(FullDate,'mm/dd/yy HH:MM:SS')
UnitTemp = DataCell{3}
AtmTemp = DataCell{4}
Humidity = DataCell{5}
fig figure1
fig1 = figure();
ax1 = axes('Parent',fig1)
grid on
axis([min(DateNumbers) max(DateNumbers) 20 30])
ylabel('Temperature \rightarrow');
title('Temperature Graph 5th October');

Can You Explain me the meaning of this line

DataCell = textscan(fid,'%s %s %fC %fC %f%*[^\n]',1329,'Delimiter',',')

This part [^\n], although i know what it is doing but still wants to know this notation.

And Also the meaning of this line.

ax1 = axes('Parent',fig1)

Thank You Much, You method is perfect one.

Log in to comment.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!