How do I transform this format 202007011030 (2020 07 01 10:30) into a readabel format?
1 view (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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
More Answers (2)
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")
0 Comments
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)
For alternative display formats, see the description of the Format property in the datetime documentation.
See Also
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!