Create a numeric variable DATE+HOUR
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
David E.S.
am 7 Feb. 2021
Bearbeitet: Adam Danz
am 7 Feb. 2021
Hi. I hope you're ok.
I write this question because I want to create a new numeric variable which contains the date (i.e. 12/09/2019) and the hour (i.e. 01:15:20). I did this in Excel and the result is given in the following photo:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/512397/image.png)
However, If I try to do this in MATLAB (R19b) the result is quite different.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/512402/image.png)
Can you tell me if I'm doing something wrong? I don't know if the sintaxis of the date and hour are correct, and if not, what's wrong with that (I want the result given in my Excel sheet).
Thanks!
David
0 Kommentare
Akzeptierte Antwort
Adam Danz
am 7 Feb. 2021
Bearbeitet: Adam Danz
am 7 Feb. 2021
There's rarely a good reason to use datenum rather than datetime values. See Why are datetime arrays better than datenum?
% Convert to datetime
Time = '01:15:02 12/09/19';
datetime(Time,'InputFormat','HH:mm:ss MM/dd/yy')
Date numbers in Excel represent the number of days since Jan 1 1900 whereas date numbers in Matlab represent the number of days since Jan 1,0000 so there's a 693960 day difference (see documentation).
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Time Series Objects 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!