How to transform a cell array, which contains dates but not in the datetime format, into datetime array

3 Ansichten (letzte 30 Tage)
Hello to everybody.
The cell array contains values like this one :
x={'1991-01-09T02:00:00+00:00' ; '1991-01-09T03:00:00+00:00' ; '1991-01-09T04:00:00+00:00 .......}
and they are always in this format.
My goal is to obtain a hourly datetime array from it in the format 'yyyy-MM-dd''T''HH' or something similar, what's important is that it has also hours, i don't absolutely need minutes and seconds, just hours.
I need this solution because the cell array comes from an external textfile (it is a simulation output), and the dates in it are not everywhere perfectly regular hour by hour, so i don't need to just create a hourly datetime array myself, but i really would like to scan that cell array in some way and create a datetime array with the ones present in the original with years-months-days and hours.
Hope that my question is clear! Thank you!

Akzeptierte Antwort

dpb
dpb am 29 Jan. 2021
Bearbeitet: dpb am 29 Jan. 2021
>> dt=datetime(tstr,'InputFormat','yyyy-MM-dd''T''hh:mm:ssXXX','TimeZone','America/Chicago')
dt =
3×1 datetime array
08-Jan-1991 20:00:00
08-Jan-1991 21:00:00
08-Jan-1991 22:00:00
>>
Fixup the time zone to match or convert to be unzoned by setting its 'TimeZone' property to '' (empty string).
>> dt.TimeZone=''
dt =
3×1 datetime array
08-Jan-1991 20:00:00
08-Jan-1991 21:00:00
08-Jan-1991 22:00:00
>>
Set format as want via:
>> dt.Format='dd-MMM-uuuu HH'
dt =
3×1 datetime array
08-Jan-1991 20
08-Jan-1991 21
08-Jan-1991 22
>>
  2 Kommentare
Enrico Gambini
Enrico Gambini am 29 Jan. 2021
It works! One only comment, the 'InputFormat should be:
'yyyy-MM-dd''T''HH:mm:ssXXX' %%capital h instead
Otherwise dates going from 12 am to 12 pm will be saved as NaT.
Thank you a lot!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

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

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by