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 件のコメント
採用された回答
Stephen23
2022 年 1 月 19 日
編集済み: Stephen23
2022 年 1 月 19 日
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
その他の回答 (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!