필터 지우기
필터 지우기

Extracting the year from a timestamp in a column

조회 수: 1 (최근 30일)
Glowworm8
Glowworm8 2018년 6월 12일
댓글: Peter Perkins 2018년 7월 5일
I have a set of 477 values in a column, each that are formatted 'yyyy-mm-dd HH:MM:SS' and am trying to extract only the year using a loop
for i=1:477
[t] = datevec(data{i,8},'yyyy-mm-dd HH:MM:SS');
't_years' = t.Year;
end

채택된 답변

Stephen23
Stephen23 2018년 6월 12일
편집: Stephen23 2018년 6월 12일
Your code confuses a date vector (which is a simple double matrix of size Nx6 or Nx3) with a datetime object (which has the property obj.Year). If you want to use that property then you will need to use a datetime object. If you want to use datvec, then you do not need a loop as it accepts a cell array of char vectors directly. So probably something like this will work (it depends on how the data is arranged in data):
mat = datevec(data(:,8),'yyyy-mm-dd HH:MM:SS');
year = mat(:,1);
  댓글 수: 2
Glowworm8
Glowworm8 2018년 6월 12일
Thank you!
Peter Perkins
Peter Perkins 2018년 7월 5일
And if you want to stick with datetimes (which you probably do),
d = datetime(data{i,8},'yyyy-MM-dd HH:mm:ss');
y = d.Year

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by