MATLAB Answers

Converting date times into periods, day, month

2 ビュー (過去 30 日間)
James McBrearty
James McBrearty 2019 年 11 月 21 日
コメント済み: James McBrearty 2019 年 11 月 21 日
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.


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


その他の回答 (0 件)




Community Treasure Hunt

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

Start Hunting!

Translated by