How to extract Datetime string to separate columns of yyyy dd mm hh mm
36 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Dennis
el 9 de En. de 2023
Comentada: Siddharth Bhutiya
el 26 de En. de 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 comentarios
Respuesta aceptada
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)
2 comentarios
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
Más respuestas (1)
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);
% 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 comentarios
Ver también
Categorías
Más información sobre Dates and Time en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!