How to extract Datetime string to separate columns of yyyy dd mm hh mm
28 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Stephen23
le 9 Jan 2023
Modifié(e) : Stephen23
le 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 commentaires
Siddharth Bhutiya
le 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
Plus de réponses (1)
Mohammad Sami
le 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 commentaires
Voir également
Catégories
En savoir plus sur Dates and Time dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!