Divide time array in day and night

7 Ansichten (letzte 30 Tage)
Emma Blanken
Emma Blanken am 29 Jun. 2018
Bearbeitet: jonas am 30 Jun. 2018
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 (:

Antworten (2)

jonas
jonas am 29 Jun. 2018
Bearbeitet: jonas am 30 Jun. 2018
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 am 30 Jun. 2018
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 Kommentare
Emma Blanken
Emma Blanken am 30 Jun. 2018
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 am 30 Jun. 2018
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by