Divide time array in day and night
7 次查看(过去 30 天)
显示 更早的评论
Hello there,
Currently, I am trying to divide a time array with has a row every hour (01:00 02-07-2014 to 01:00 02-07-2016, 17545 rows) into day and night, i.e. two subarrays.
Thus, all the moments between 20.00 - 8.00 are night, and between 8.00 - 20.00 are day. Thus the first, 7 hours of the array should be counted as night, and from there on it should just 'select' the following 12 hours, put it in the day subarray, then the following 12 hours and put it in the night array and so on until the end (where the last 5 points should be put in the night array again).
Is there a way to do this? Thanks in advance (:
0 个评论
回答(2 个)
jonas
2018-6-29
编辑:jonas
2018-6-30
There are many ways to do this. It is quite trivial in datetime format
%This is your time vector
time=datetime('2014-2-7 01:00')+hours(1:17545)';
%Extract rows where the hour-of-day is between 8 and 20
day=time(hour(time)>=8 & hour(time)<=20)
%Extract rows where the hour-of-day is between 20 and 8
night=time(hour(time)<8 | hour(time)>20)
If you have multiple variables associated with each time slot, then I propose you organize your data in a timetable:
TT=timetable(time,var1,var2,var3...)
Timetable also works with datetime format so you can still perform the above operations. You will find it extremely useful if you have multi-variable datasets.
0 个评论
Shantanu Gontia
2018-6-30
MATLAB provides the datetime format for handling Calendar information. You can initialize a datetime array by providing the initial and final dates and use duration object of 1 hour to provide the step-size. Here is a sample snippet of code to perform this.
first = datetime('01:00 02-07-2014', 'InputFormat', 'hh:mm dd-MM-yyyy'); % Get First Date
last = datetime('01:00 02-07-2016', 'InputFormat', 'hh:mm dd-MM-yyyy'); % Get Final Date
dates = first:hours(1):last; % Create an array of dates separated by 1 hour
The function hours(1) produces a duration object of 1 hour. Now, you can index the array dates using the Hour field.
daytime_dates = dates(dates.Hour < 20 && dates.Hour >= 8);
daytime_dates will contain the required dates.
2 个评论
Shantanu Gontia
2018-6-30
Yes, if you have the temperature data for the same times, you can use the same condition dates.Hour < 20 && dates.Hour >= 8, to index the variable holding the temperature. This will give you the daytime temperatures.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!