How to extract Datetime string to separate columns of yyyy dd mm hh mm
17 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Dennis
am 9 Jan. 2023
Kommentiert: Siddharth Bhutiya
am 26 Jan. 2023
Am a noob, so bear with me, I need to extract the date string and separate the year day month hour and minute to separate column within the dataframe. AM getting an error with ectractBetween. What could I be doing wrong? Ive started with just the year in this code
ararat = importdata("ararat.txt");
Q1 = [ararat(:,1)];
resQ1 = num2str(Q1)
for N=1:length(resQ1)
year = extractBetween(resQ1, 1,4)
end
0 Kommentare
Akzeptierte Antwort
Stephen23
am 9 Jan. 2023
Bearbeitet: Stephen23
am 9 Jan. 2023
That is a very unfortunate date format. Best avoided.
T = readtable('ararat.txt', 'Format','%{yyyyddMMHHmm}D%f'); % ugh.
T.Properties.VariableNames = {'DT','Val'};
T.DT.Format = 'yyyy-MM-dd HH:mm:ss'; % aaah, much better :)
[T.Year,T.Month,T.Day] = ymd(T.DT);
[T.Hour,T.Minute,T.Second] = hms(T.DT);
display(T)
2 Kommentare
Siddharth Bhutiya
am 26 Jan. 2023
You dont even need to call ymd or hms here. For example if DT is a datetime then doing DT.Year would give you the year of all datetime, DT.Month, the month and so on. So this could be done like this.
T.Year = T.DT.Year;
T.Month = T.DT.Month; % and so on
Weitere Antworten (1)
Mohammad Sami
am 9 Jan. 2023
varNames = {'dt','val'} ;
varTypes = {'datetime','double'} ;
delimiter = '\t';
opts = delimitedTextImportOptions('VariableNames',varNames,...
'VariableTypes',varTypes,...
'Delimiter',delimiter);
% specify the datetime input format to read the column as datetime value
opts = setvaropts(opts,{'dt'},'InputFormat','yyyyddMMHHmm');
a = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1256912/ararat.txt',opts);
head(a);
% use datevec function to extract year, month, date, hour and minute values
[a.y,a.M,a.d,a.H,a.m] = datevec(a.dt);
head(a);
0 Kommentare
Siehe auch
Kategorien
Mehr zu Dates and Time finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!