Converting date times into periods, day, month

4 visualizaciones (últimos 30 días)
James McBrearty
James McBrearty el 21 de Nov. de 2019
Comentada: James McBrearty el 21 de Nov. de 2019
Hi,
I have a list of datetimes, and I want to convert it to Month No., Day No. Period No. Where Period No. is the number of the half hour in the day, where 00:00 is Period 1. and 23:30 is Period 48. An example of the datetimes is as follows:
Ideally I will want to have 3 resultant columns. This is used for determining how things would be expected to run at a given period/day/month. There are other structures besides this, but this will help me on my way. Is there any simple way of doing this? I did use a couple of loops from an old way, but I am getting the data from a database now, rather than a flat file so I want to make it more efficient.
Thanks,
James

Respuesta aceptada

meghannmarie
meghannmarie el 21 de Nov. de 2019
I would try something like this:
T = table();
[~,T.Month,T.Day] = ymd(FC.StartDateTime);
[h,m] = hms(FC.StartDateTime);
[~,T.period] = ismember((h +(m/60)),(0:.5:24));
  1 comentario
James McBrearty
James McBrearty el 21 de Nov. de 2019
Hi this was really good. Thanks. I had got something similar, by using:
FCStruct.Month = FCStruct.StartDateTime.Month;
FCStruct.Day = FCStruct.StartDateTime.Day;
FCStruct.Hour = FCStruct.StartDateTime.Hour;
FCStruct.Hour = (FCStruct.Hour+1)*2;
for i=1:length(FCStruct.Hour)
if mod(i,2)~=0
FCStruct.Hour(i) = FCStruct.Hour(i)-1;
end
end
But I think your code is more efficient and doesn't rely on a loop.
Thanks, James

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by