How do I transform this format 202007011030 (2020 07 01 10:30) into a readabel format?

4 views (last 30 days)
Hello,
I have a txt-file with date in the format 202007041230 and I can figure out, how to seperate it in order to get a readable format.
I tried to just reproduce the dates and time by myself, because I have the beginning date and time and the ending date and time and know, that every hour a new measurement was taken. But some hours are randomly missing, so the reproducing isn't an easy way around it.
And I'm convinced, that it should be not that hard to seperate this format, but I seem to miss the key information.
Thank you in advance for you help. It will help me out alot.

Accepted Answer

Stephen23
Stephen23 on 19 Jan 2022
Edited: Stephen23 on 19 Jan 2022
You can easily import as DATETIME, no need to import as text nor fiddle around with text:
fid = fopen('Fehmarn_Wind_date.txt','rt');
out = textscan(fid,'%{yyyyMMddHHmm}D');
fclose(fid);
out = out{1};
out.Format = 'yyyy-MM-dd HH:mm' % pick any format you want
out = 1810×1 datetime array
2020-07-01 00:29 2020-07-01 01:29 2020-07-01 02:29 2020-07-01 03:29 2020-07-01 04:29 2020-07-01 05:29 2020-07-01 06:29 2020-07-01 07:29 2020-07-01 08:29 2020-07-01 09:29 2020-07-01 10:29 2020-07-01 11:29 2020-07-01 12:29 2020-07-01 13:29 2020-07-01 14:29 2020-07-01 15:29 2020-07-01 16:29 2020-07-01 17:29 2020-07-01 18:29 2020-07-01 19:29 2020-07-01 20:29 2020-07-01 21:29 2020-07-01 22:29 2020-07-01 23:29 2020-07-02 00:29 2020-07-02 01:29 2020-07-02 02:29 2020-07-02 03:29 2020-07-02 04:29 2020-07-02 05:29
  1 Comment
Sarah Preuß
Sarah Preuß on 20 Jan 2022
Thank you for this quick and easy fix.
One question: Why is ther a D after this expression {yyyyMMddHHmm}?

Sign in to comment.

More Answers (2)

Cris LaPierre
Cris LaPierre on 19 Jan 2022
Edited: Cris LaPierre on 19 Jan 2022
I think you would first need to read in the dates as strings, and then use datetime to convert them to datetimes. It seems when the date and time do not have any delimiters, the conversion to datetime cannot be done as part of the import.
opts = detectImportOptions("Fehmarn_Wind_date.txt");
opts = setvartype(opts,1,"string");
data = readtable("Fehmarn_Wind_date.txt",opts);
data.Var1 = datetime(data.Var1,"InputFormat","yyyyMMddHHmm")
data = 1810×1 table
Var1 ____________________ 01-Jul-2020 00:29:00 01-Jul-2020 01:29:00 01-Jul-2020 02:29:00 01-Jul-2020 03:29:00 01-Jul-2020 04:29:00 01-Jul-2020 05:29:00 01-Jul-2020 06:29:00 01-Jul-2020 07:29:00 01-Jul-2020 08:29:00 01-Jul-2020 09:29:00 01-Jul-2020 10:29:00 01-Jul-2020 11:29:00 01-Jul-2020 12:29:00 01-Jul-2020 13:29:00 01-Jul-2020 14:29:00 01-Jul-2020 15:29:00

Steve Eddins
Steve Eddins on 19 Jan 2022
I can imagine several ways to do this. Here is one.
lines = readlines("Fehmarn_Wind_date.txt");
readlines was introduced in R2020b. Alternative code for earlier releases:
lines = split(string(fileread("Fehmarn_Wind_date.txt")),newline);
lines(lines == "") = [];
yr = str2double(extractBetween(lines,1,4));
mon = str2double(extractBetween(lines,5,6));
day = str2double(extractBetween(lines,7,8));
hr = str2double(extractBetween(lines,9,10));
min = str2double(extractBetween(lines,11,12));
sec = zeros(size(min));
time_stamps = datetime(yr,mon,day,hr,min,sec)
time_stamps = 1810×1 datetime array
01-Jul-2020 00:29:00 01-Jul-2020 01:29:00 01-Jul-2020 02:29:00 01-Jul-2020 03:29:00 01-Jul-2020 04:29:00 01-Jul-2020 05:29:00 01-Jul-2020 06:29:00 01-Jul-2020 07:29:00 01-Jul-2020 08:29:00 01-Jul-2020 09:29:00 01-Jul-2020 10:29:00 01-Jul-2020 11:29:00 01-Jul-2020 12:29:00 01-Jul-2020 13:29:00 01-Jul-2020 14:29:00 01-Jul-2020 15:29:00 01-Jul-2020 16:29:00 01-Jul-2020 17:29:00 01-Jul-2020 18:29:00 01-Jul-2020 19:29:00 01-Jul-2020 20:29:00 01-Jul-2020 21:29:00 01-Jul-2020 22:29:00 01-Jul-2020 23:29:00 02-Jul-2020 00:29:00 02-Jul-2020 01:29:00 02-Jul-2020 02:29:00 02-Jul-2020 03:29:00 02-Jul-2020 04:29:00 02-Jul-2020 05:29:00
For alternative display formats, see the description of the Format property in the datetime documentation.
  1 Comment
Sarah Preuß
Sarah Preuß on 20 Jan 2022
Thank you so much for your help!
Now I can use it to display the dates, when I'm plotting the time series.

Sign in to comment.

Categories

Find more on Dates and Time 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!