How to avoid "NaT" when converting a cell into datetime ? (Or: how to add missing parts of datetime ?)

13 Ansichten (letzte 30 Tage)
How to avoid "NaT" when converting a cell into datetime ?
(Or: how to add missing parts of datetime ?)
a = [
{'14-Jul-2021 17:00:00'}
{'15-Jul-2021 12:00:00'}
{'16-Jul-2021 21:00:00'}
{'17-Jul-2021' }
{'18-Jul-2021 06:00:00'}
{'19-Jul-2021 04:00:00'}
{'20-Jul-2021 17:00:00'}
{'21-Jul-2021 08:00:00'}]
a = 8×1 cell array
{'14-Jul-2021 17:00:00'} {'15-Jul-2021 12:00:00'} {'16-Jul-2021 21:00:00'} {'17-Jul-2021' } {'18-Jul-2021 06:00:00'} {'19-Jul-2021 04:00:00'} {'20-Jul-2021 17:00:00'} {'21-Jul-2021 08:00:00'}
datetime(a)
ans = 8×1 datetime array
14-Jul-2021 17:00:00 15-Jul-2021 12:00:00 16-Jul-2021 21:00:00 NaT 18-Jul-2021 06:00:00 19-Jul-2021 04:00:00 20-Jul-2021 17:00:00 21-Jul-2021 08:00:00
As workaround, how to add a representative hour (e.g. '12:00:00') where it is missing, in order to avoid a "NaT" ?

Akzeptierte Antwort

Stephen23
Stephen23 am 12 Aug. 2022
Bearbeitet: Stephen23 am 12 Aug. 2022
"How to avoid "NaT" when converting a cell into datetime ?"
Convert each cell individually. Certainly not as efficient as one DATETIME call, but it is fairly simple:
C = {...
'14-Jul-2021 17:00:00'
'15-Jul-2021 12:00:00'
'16-Jul-2021 21:00:00'
'17-Jul-2021'
'18-Jul-2021 06:00:00'
'19-Jul-2021 04:00:00'
'20-Jul-2021 17:00:00'
'21-Jul-2021 08:00:00'}; % one cell array :)
D = cellfun(@datetime,C)
D = 8×1 datetime array
14-Jul-2021 17:00:00 15-Jul-2021 12:00:00 16-Jul-2021 21:00:00 17-Jul-2021 00:00:00 18-Jul-2021 06:00:00 19-Jul-2021 04:00:00 20-Jul-2021 17:00:00 21-Jul-2021 08:00:00
  5 Kommentare
Stephen23
Stephen23 am 12 Aug. 2022
"but the thing is that the position can change"
Of course, you can use any indexing you want, you are not restricted to just using index 4. For example, if you want to detect all character vectors shorter than 12 characters and add 12 hours to the corresponding datetime:
C = {...
'14-Jul-2021 17:00:00'
'15-Jul-2021 12:00:00'
'16-Jul-2021 21:00:00'
'17-Jul-2021'
'18-Jul-2021 06:00:00'
'19-Jul-2021 04:00:00'
'20-Jul-2021 17:00:00'
'21-Jul-2021 08:00:00'}; % one cell array :)
D = cellfun(@datetime,C);
X = cellfun(@numel,C)<12; % or whatever logic you want to use (which you did not explain)
D(X) = D(X) + hours(12)
D = 8×1 datetime array
14-Jul-2021 17:00:00 15-Jul-2021 12:00:00 16-Jul-2021 21:00:00 17-Jul-2021 12:00:00 18-Jul-2021 06:00:00 19-Jul-2021 04:00:00 20-Jul-2021 17:00:00 21-Jul-2021 08:00:00
Sim
Sim am 12 Aug. 2022
Bearbeitet: Sim am 12 Aug. 2022
Cool, thanks!! :-)
The logic of indexing the missing parts of datetime elements by using the "characters length of those datetime elements" (i.e. 11 characters in '17-Jul-2021'), works well in my case, many thanks !

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

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