필터 지우기
필터 지우기

Divide time array in day and night

조회 수: 10 (최근 30일)
Emma Blanken
Emma Blanken 2018년 6월 29일
편집: jonas 2018년 6월 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 (:

답변 (2개)

jonas
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.

Shantanu Gontia
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
Emma Blanken
Emma Blanken 2018년 6월 30일
Thank you very much!
Then, when I have data which is assigned to each hour, can i sort it in a likewise way? (I have a temperature measurement at every hour, and want to seperate it now in day temperatures and night temperatures)
Shantanu Gontia
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 CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by