Converting a string to datenum format

조회 수: 28 (최근 30일)
Malavan Subramaniam
Malavan Subramaniam 2019년 10월 28일
답변: Shubham Gupta 2019년 10월 29일
So I have a dataset that presents dates as strings (example: "2011-08-31T21:16:01.948Z"). My question is how do I convert this string into a datenum format? I feel like the 'T' and the 'Z' might make things more complicated. Also, how would I do this for every date in the dataset?
I am very new and bad with Matlab, so please provide as simple as an explanation that you can! That would be greatly appreciated! :)
Thank you!!

답변 (2개)

Walter Roberson
Walter Roberson 2019년 10월 29일
Use datetime() to parse the string and then datenum() the results.
In most cases you are better just using the datetime object without datenum()

Shubham Gupta
Shubham Gupta 2019년 10월 29일
Try:
DateString = '2011-08-31T21:16:01.948Z';
RepExp = '[a-zA-Z]'; % want to replace any alphabet, indicated by [a-zA-Z]
RepStr = ' '; % Replace by blank space
DateString_Up = regexprep(DateString,RepExp,RepStr); % Updated date string
DateNumber = datenum(DateString_Up); % Convert it to datenum
Let me know if you have doubts !

카테고리

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