필터 지우기
필터 지우기

Using datenum for fraction of a second

조회 수: 22 (최근 30일)
Bruno Rodriguez
Bruno Rodriguez 2017년 3월 10일
댓글: Walter Roberson 2017년 3월 11일
I am looking for a ways to convert some dates into datenum, given their special format. My dates look as follows:
'2017-02-28 21:36:51'
'2017-02-28 21:36:51.5'
'2017-02-28 21:36:52'
'2017-02-28 21:36:52.5'
'2017-02-28 21:36:53'
'2017-02-28 21:36:53.5'
etc. etc. etc.
In other words, the format of the dates alternates, such that every other entry has an additional 500 milliseconds at the end of it. How can I convert these to datenum simultaneously, given their different formats? (or at least, how can I convert dates with .5 to datenum?). I cannot skip or ignore any of this data.
Thanks
  댓글 수: 1
Walter Roberson
Walter Roberson 2017년 3월 10일
Would datetime format instead of datenum format be acceptable?
datenum() tends to be more flexible in format conversion but sometimes datetime() is easier.

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

채택된 답변

Walter Roberson
Walter Roberson 2017년 3월 10일
dates = {'2017-02-28 21:36:51'
'2017-02-28 21:36:51.5'
'2017-02-28 21:36:52'
'2017-02-28 21:36:52.5'
'2017-02-28 21:36:53'
'2017-02-28 21:36:53.5'};
temp = regexprep(dates, ':\d\d$', '$&.0', 'lineanchors');
Now temp is in consistent format
  댓글 수: 2
Bruno Rodriguez
Bruno Rodriguez 2017년 3월 10일
Thank you, that certainly helps to make them consistent. However, I'm still unsure how to apply datenum to such a format. I seem to be getting two different datenum outputs for the first two cases above, but when I convert the second one back to datestr to double check, it still returns 21:36:51, even though it has a .5
Walter Roberson
Walter Roberson 2017년 3월 11일
datestr(datenum(temp), 'YYYY-mm-DD hh:MM:ss.fff')

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by