Formatting and plotting downloaded data

1 view (last 30 days)
Lucien Hollins
Lucien Hollins on 1 Jun 2017
Answered: Nirav Sharda on 12 Jun 2017
Hi.
I have some hourly readings over the course of several months.
The data looks like this in the .txt file:
01/01/2017 00:00
780.70
01/01/2017 01:00
780.71
01/01/2017 02:00
780.74
Note that the time is 24-hr format with the date and time on one row and the reading on the row below. I have tried several methods with varying results to make this data useful including the Import Data tool, which I don't fully understand.
All I want to do is find an acceptable way of using this data to generate a nice looking plot with the date and time clearly visible on the x-axis and the reading on the y-axis.
Thank you.

Answers (1)

Nirav Sharda
Nirav Sharda on 12 Jun 2017
This can be achieved by using Low-Level File I/O functions like fopen,fgetl etc. I am creating a small script that can be used as a reference.
% open the file for reading
f = fopen('filename');
time = {};
values = [];
k = 1;
% The idea is to keep reading lines till the end of file is reached
% The lines are grouped in three line groups: firs line date, second line
% value and blank line to be skipped.
while ~feof(f)
% read the first line
line = fgetl(f);
% add it to the cell-array
time{k} = line;
% read the second line containing the value
line = fgetl(f);
% add it to the matrix
values(k) = str2num(line);
% skip the blank line
line = fgetl(f);
k = k + 1;
end
% convert cell-array to datetime array
time = datetime(time,'InputFormat','MM/dd/uuuu HH:mm');
% plotting
plot(time,values)
% close the file
fclose(f)
I hope this helps.

Community Treasure Hunt

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

Start Hunting!