How do I transform this format 202007011030 (2020 07 01 10:30) into a readabel format?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Stephen23
le 19 Jan 2022
Modifié(e) : Stephen23
le 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
Plus de réponses (2)
Cris LaPierre
le 19 Jan 2022
Modifié(e) : Cris LaPierre
le 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 commentaires
Steve Eddins
le 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.
Voir également
Catégories
En savoir plus sur Dates and Time dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!