Converting date times into periods, day, month

5 views (last 30 days)
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.

Accepted Answer

meghannmarie on 21 Nov 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 Comment
James McBrearty
James McBrearty on 21 Nov 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;
But I think your code is more efficient and doesn't rely on a loop.
Thanks, James

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!

Translated by