Matlab not recognizing dates

1 Ansicht (letzte 30 Tage)
Jose Vazquez
Jose Vazquez am 23 Nov. 2020
Kommentiert: Steven Lord am 23 Nov. 2020
I have a vector of dates in the format yyyymm, however matlab is not recognizing it for a timetable. Any suggestions?

Akzeptierte Antwort

Star Strider
Star Strider am 23 Nov. 2020
You did not mention how you were importing them.
Try importing them this way:
dv = {'202011'
'202012'}
datetime(dv, 'InputFormat','yyyyMM', 'Format','MM-yyyy')
That works for me.
  3 Kommentare
Star Strider
Star Strider am 23 Nov. 2020
That is similar to changing header to csv using writetable with a similar solution:
dv = [202011
202012];
DT = datetime(num2str(dv), 'InputFormat','yyyyMM', 'Format','MM-yyyy')
producing:
DT =
2×1 datetime array
11-2020
12-2020
Or if you want them in the same format as the input:
DT = datetime(num2str(dv), 'InputFormat','yyyyMM', 'Format','yyyyMM')
producing:
DT =
2×1 datetime array
202011
202012
.
Steven Lord
Steven Lord am 23 Nov. 2020
You can skip the num2str with a little bit of arithmetic.
x = (202001:202006).'
x = 6×1
202001 202002 202003 202004 202005 202006
DT = datetime(100*x+1, 'ConvertFrom', 'yyyymmdd')
DT = 6×1 datetime array
01-Jan-2020 01-Feb-2020 01-Mar-2020 01-Apr-2020 01-May-2020 01-Jun-2020

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

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

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by