How to extract Datetime string to separate columns of yyyy dd mm hh mm
조회 수: 36 (최근 30일)
이전 댓글 표시
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
채택된 답변
Stephen23
2023년 1월 9일
편집: Stephen23
2023년 1월 9일
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
Siddharth Bhutiya
2023년 1월 26일
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
추가 답변 (1개)
Mohammad Sami
2023년 1월 9일
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
참고 항목
카테고리
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!