How to use datenum with a date represented as a scaler?
3 views (last 30 days)
I have data I need to ingest that is date specific however, the data is represented as a scaler e.g. 20180924 with no spaces or hyphens. How do I get datenum to take this date in and separate it out into a vector so I can use it?
Kelly Kearney on 24 Sep 2018
Is your final goal to convert to a datenumber or an year-month-day array? As mentioned in the other answers, if you're using a recent version of Matlab, datetimes are more flexible than datenumbers. Once you've converted the value to a datetime, you can move between datenumbers and datevectors pretty easily:
x = ;
>> t = datetime(num2str(x, '%08d'), 'inputFormat', 'yyyyMMdd')
2018 9 24 0 0 0
More Answers (3)
ANKUR KUMAR on 24 Sep 2018
If A is numeric, then
If you wish to store date in vector, then
Walter Roberson on 24 Sep 2018
datetime(TheScalar, 'ConvertFrom', 'yyyymmdd')
You can convert the result to datenum if you insist: just double() the datetime
Peter Perkins on 1 Oct 2018
If possible, use datetime rather than datenum/datestr/datevec. As Walter showed, you can convert directly from numeric to datetime. It's also possible that you do not need to store the separate pieces, since datetime lets you get at them any time you want, as a property.