Converting date times into periods, day, month

3 次查看(过去 30 天)
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

采纳的回答

meghannmarie
meghannmarie 2019-11-21
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 个评论
James McBrearty
James McBrearty 2019-11-21
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by