How to use datenum with a date represented as a scaler?

1 Ansicht (letzte 30 Tage)
Eric Metzger
Eric Metzger am 24 Sep. 2018
Beantwortet: Peter Perkins am 1 Okt. 2018
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?
  3 Kommentare
Eric Metzger
Eric Metzger am 24 Sep. 2018
Neither. It is a numeric scaler. I am using R2018a.
Stephen23
Stephen23 am 24 Sep. 2018
@Eric Metzger: do you need to convert one such integer, or multiple integers?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Kelly Kearney
Kelly Kearney am 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 = [20180924];
>> t = datetime(num2str(x, '%08d'), 'inputFormat', 'yyyyMMdd')
t =
datetime
24-Sep-2018
>> datenum(t)
ans =
737327
>> datevec(t)
ans =
2018 9 24 0 0 0
  4 Kommentare
Kelly Kearney
Kelly Kearney am 24 Sep. 2018
Specifically in the this case, it makes sure the answer is robust to dates earlier than year 1000 by padding any less-than-4-digit years with zeros. (Datetime is actually smart enough to figure things out even without the padding... but I like to be explicit just in case.) Note that this will fail if you're dealing with any BC dates, but I'm assuming you would have specified if that was the case.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

ANKUR KUMAR
ANKUR KUMAR am 24 Sep. 2018
If A is numeric, then
A=20180924
a1=num2str(A)
datenum(str2double({a1(1:4),a1(5:6),a1(7:8)}));
If you wish to store date in vector, then
A1=[str2double({a1(1:4),a1(5:6),a1(7:8)})]
  6 Kommentare
Stephen23
Stephen23 am 24 Sep. 2018
Note that the square brackets around str2double are totally superfluous:
Eric Metzger
Eric Metzger am 24 Sep. 2018
Okay, this will break it up into three separate cell, but I get this error when I try to use datenum on it:
A1 =
1×3 cell array
{'2018'} {'09'} {'24'}
>> datenum(A1) Error using datenum (line 189) DATENUM failed.
Caused by: Error using datevec (line 281) Cannot parse date 2018.
I am not use to working with the {} brackets for arrays/matrices but the [] e.g [2018 9 24 18 15 56] where it is the yyyy mm dd hh mm ss. The "three cell array" is not working the way I normally use it. This is close to not quite the cigar yet.

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 24 Sep. 2018
datetime(TheScalar, 'ConvertFrom', 'yyyymmdd')
You can convert the result to datenum if you insist: just double() the datetime
  3 Kommentare
Steven Lord
Steven Lord am 24 Sep. 2018
I would use the ymd function or the datevec function on the datetime object.
theScalar = 20180924
dt = datetime(theScalar, 'ConvertFrom', 'yyyymmdd')
[y, m, d] = ymd(dt)
DV = datevec(dt)
You can even go from the date vector back to a datetime.
dt2 = datetime(DV)
isequal(dt, dt2)
Walter Roberson
Walter Roberson am 24 Sep. 2018
It isn't clear what result is being looked for. If it is separated parts, with the implementation of sprintf -> str2num -> datenum -> datevec being considered, then my numeric code computes the pieces directly without needing any calls more expensive then mod.

Melden Sie sich an, um zu kommentieren.


Peter Perkins
Peter Perkins am 1 Okt. 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.

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by