Convert DoY and hour to datetime format

28 visualizaciones (últimos 30 días)
Benju Baniya
Benju Baniya el 1 de Jul. de 2022
Comentada: Benju Baniya el 1 de Jul. de 2022
I am very new to matlab and I am working with timeseries data. In the attached example, I want to add a new column as Datetime that displays datetime in the format year, month, day, hour, minute (YYYYMMDDHHMM). Your help will be much appreciated. Thanks.
Year DoY Hour Datetime
2013 1 0.5 201301010030

Respuesta aceptada

Abhishek Tiwari
Abhishek Tiwari el 1 de Jul. de 2022
Hi,
It can be done by using datetime() with modified display format as demonstrated
T = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1053045/example.xlsx');
T.Datetime = datetime(T.Year, 1, T.DoY, floor(T.Hour), rem(T.Hour, 1).*60, 0, 'format', 'yyyyMMddHHmm')
T = 87648×4 table
Year DoY Hour Datetime ____ ___ ____ ____________ 2013 1 0.5 201301010030 2013 1 1 201301010100 2013 1 1.5 201301010130 2013 1 2 201301010200 2013 1 2.5 201301010230 2013 1 3 201301010300 2013 1 3.5 201301010330 2013 1 4 201301010400 2013 1 4.5 201301010430 2013 1 5 201301010500 2013 1 5.5 201301010530 2013 1 6 201301010600 2013 1 6.5 201301010630 2013 1 7 201301010700 2013 1 7.5 201301010730 2013 1 8 201301010800
These might be useful:
  1. Arrays that represent points in time - MATLAB (mathworks.com)
  2. Set Date and Time Display Format - MATLAB & Simulink (mathworks.com)
  1 comentario
Benju Baniya
Benju Baniya el 1 de Jul. de 2022
Thank you. It worked. This was the exact format I was looking for.

Iniciar sesión para comentar.

Más respuestas (1)

Eric Sofen
Eric Sofen el 1 de Jul. de 2022
The datetime constructor is forgiving about day values that don't fit within a particular month and wrapping them appropriately:
t = readtable("https://www.mathworks.com/matlabcentral/answers/uploaded_files/1053045/example.xlsx")
t = 87648×3 table
Year DoY Hour ____ ___ ____ 2013 1 0.5 2013 1 1 2013 1 1.5 2013 1 2 2013 1 2.5 2013 1 3 2013 1 3.5 2013 1 4 2013 1 4.5 2013 1 5 2013 1 5.5 2013 1 6 2013 1 6.5 2013 1 7 2013 1 7.5 2013 1 8
t.DT = datetime(t.Year,1,t.DoY) + hours(t.Hour)
t = 87648×4 table
Year DoY Hour DT ____ ___ ____ ____________________ 2013 1 0.5 01-Jan-2013 00:30:00 2013 1 1 01-Jan-2013 01:00:00 2013 1 1.5 01-Jan-2013 01:30:00 2013 1 2 01-Jan-2013 02:00:00 2013 1 2.5 01-Jan-2013 02:30:00 2013 1 3 01-Jan-2013 03:00:00 2013 1 3.5 01-Jan-2013 03:30:00 2013 1 4 01-Jan-2013 04:00:00 2013 1 4.5 01-Jan-2013 04:30:00 2013 1 5 01-Jan-2013 05:00:00 2013 1 5.5 01-Jan-2013 05:30:00 2013 1 6 01-Jan-2013 06:00:00 2013 1 6.5 01-Jan-2013 06:30:00 2013 1 7 01-Jan-2013 07:00:00 2013 1 7.5 01-Jan-2013 07:30:00 2013 1 8 01-Jan-2013 08:00:00
% Check that it looks reasonable further along...
t(1000:100:end,:)
ans = 867×4 table
Year DoY Hour DT ____ ___ ____ ____________________ 2013 21 20 21-Jan-2013 20:00:00 2013 23 22 23-Jan-2013 22:00:00 2013 26 0 26-Jan-2013 00:00:00 2013 28 2 28-Jan-2013 02:00:00 2013 30 4 30-Jan-2013 04:00:00 2013 32 6 01-Feb-2013 06:00:00 2013 34 8 03-Feb-2013 08:00:00 2013 36 10 05-Feb-2013 10:00:00 2013 38 12 07-Feb-2013 12:00:00 2013 40 14 09-Feb-2013 14:00:00 2013 42 16 11-Feb-2013 16:00:00 2013 44 18 13-Feb-2013 18:00:00 2013 46 20 15-Feb-2013 20:00:00 2013 48 22 17-Feb-2013 22:00:00 2013 51 0 20-Feb-2013 00:00:00 2013 53 2 22-Feb-2013 02:00:00

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by