Converting date times into periods, day, month

조회 수: 4 (최근 30일)
James McBrearty
James McBrearty 2019년 11월 21일
댓글: James McBrearty 2019년 11월 21일
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