How do I transform this format 202007011030 (2020 07 01 10:30) into a readabel format?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
更多回答(2 个)
Cris LaPierre
2022-1-19
编辑:Cris LaPierre
2022-1-19
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 个评论
Steve Eddins
2022-1-19
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!