MATLAB: How to read data from a txt file and plot them with dates on the axes

dead datatxt file

Good morning to all
I have the rain data of a rain gauge recorded as a txt file. the file contains the dates, months, hours, minutes and data. my problem is to be able to plot these data according to a precise period and especially to display on the x-axis the time that suits.
I'm new to matlab and I've been trying it for a week now but I always get something blocking me.
I would appreciate any help with this!

Best Answer

You can use readmatrix to read in your text file. I've chosen to read the date and time in as character values and then I join the date and time to create a datetime column. When you plot with datetime values your axes will show the dates in date format.
% read in the data
filename='pluie-iut.txt';
varNames = {'date','time','value'}; % name your variables here
varTypes = {'char','char','double'};
delimiter = ';';
dataStartLine = 2;
extraColRule = 'ignore';
opts = delimitedTextImportOptions('VariableNames',varNames,...
'VariableTypes',varTypes,...
'Delimiter',delimiter,...
'DataLines', dataStartLine,...
'ExtraColumnsRule',extraColRule);
T = readtable(filename,opts);
Look at the first few rows of the table
head(T)
ans =
8×3 table
date time value
____________ ____________ _____
{'20180703'} {'14:50:00'} 0
{'20180704'} {'12:55:00'} 0
{'20180704'} {'13:00:00'} 0
{'20180704'} {'20:40:00'} 0
{'20180704'} {'20:45:00'} 0.4
{'20180704'} {'20:50:00'} 0
{'20180704'} {'20:55:00'} 0
{'20180704'} {'21:00:00'} 0.2
Compute datetime values.
% add datetime column
T.datetime = datetime(strcat(T.date, {' '},T.time), 'Inputformat', 'yyyyMMdd HH:mm:ss');
View the results (first few rows)
head(T)
ans =
8×4 table
date time value datetime
____________ ____________ _____ ____________________
{'20180703'} {'14:50:00'} 0 03-Jul-2018 14:50:00
{'20180704'} {'12:55:00'} 0 04-Jul-2018 12:55:00
{'20180704'} {'13:00:00'} 0 04-Jul-2018 13:00:00
{'20180704'} {'20:40:00'} 0 04-Jul-2018 20:40:00
{'20180704'} {'20:45:00'} 0.4 04-Jul-2018 20:45:00
{'20180704'} {'20:50:00'} 0 04-Jul-2018 20:50:00
{'20180704'} {'20:55:00'} 0 04-Jul-2018 20:55:00
{'20180704'} {'21:00:00'} 0.2 04-Jul-2018 21:00:00
Plot the data
plot(T.datetime, T.value, 'o')