How to extract Datetime string to separate columns of yyyy dd mm hh mm

17 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Stephen23
Stephen23 el 9 de En. de 2023
Editada: Stephen23 el 9 de En. de 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)
T = 52560×8 table
DT Val Year Month Day Hour Minute Second ___________________ ______ ____ _____ ___ ____ ______ ______ 2020-03-21 12:00:00 3.9798 2020 3 21 12 0 0 2020-03-21 12:10:00 4.3032 2020 3 21 12 10 0 2020-03-21 12:20:00 4.2702 2020 3 21 12 20 0 2020-03-21 12:30:00 4.0128 2020 3 21 12 30 0 2020-03-21 12:40:00 4.9302 2020 3 21 12 40 0 2020-03-21 12:50:00 5.445 2020 3 21 12 50 0 2020-03-21 13:00:00 4.983 2020 3 21 13 0 0 2020-03-21 13:10:00 3.8544 2020 3 21 13 10 0 2020-03-21 13:20:00 7.2072 2020 3 21 13 20 0 2020-03-21 13:30:00 7.557 2020 3 21 13 30 0 2020-03-21 13:40:00 7.6098 2020 3 21 13 40 0 2020-03-21 13:50:00 7.0686 2020 3 21 13 50 0 2020-03-21 14:00:00 6.666 2020 3 21 14 0 0 2020-03-21 14:10:00 6.0588 2020 3 21 14 10 0 2020-03-21 14:20:00 5.8674 2020 3 21 14 20 0 2020-03-21 14:30:00 5.7156 2020 3 21 14 30 0
  2 comentarios
Dennis
Dennis el 18 de En. de 2023
this is awesome, way easier to implement and extract the timeframes
Siddharth Bhutiya
Siddharth Bhutiya el 26 de En. de 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

Iniciar sesión para comentar.

Más respuestas (1)

Mohammad Sami
Mohammad Sami el 9 de En. de 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);
dt val ____________ ______ 202021031200 3.9798 202021031210 4.3032 202021031220 4.2702 202021031230 4.0128 202021031240 4.9302 202021031250 5.445 202021031300 4.983 202021031310 3.8544
% 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);
dt val y M d H m ____________ ______ ____ _ __ __ __ 202021031200 3.9798 2020 3 21 12 0 202021031210 4.3032 2020 3 21 12 10 202021031220 4.2702 2020 3 21 12 20 202021031230 4.0128 2020 3 21 12 30 202021031240 4.9302 2020 3 21 12 40 202021031250 5.445 2020 3 21 12 50 202021031300 4.983 2020 3 21 13 0 202021031310 3.8544 2020 3 21 13 10

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by