- If the start time is before sunrise and endtime is after sunset, does that count as day or night or both?
- If start time is after sunrise but the end time is after sunset, should that row be day or night?
divide year time data into days and nights
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Petr Michalek
le 22 Sep 2022
Commenté : Eric Sofen
le 23 Sep 2022
Hello,
I have a table of time data for entire year and I would like to divide them into days and nights. The table rows look like that (Time_begin Time_end other_variables), example of one row: (14-Mar-2017 12:10 14-Mar-2017 12:20 var1 var2 var3 ... ). I have a table of sunrises and sunsets (day_no sunrise sunset, Example: 1 01-Jan-2017 08:01:00 01-Jan-2017 16:10:00) for the entire year and I would like to use it for dividing the data table into tables with rows with day data and night data. The sunrise and sunset time change for every day. How can I do this? Thanks for you ideas.
2 commentaires
Harry Laing
le 22 Sep 2022
I think you need to be a bit clearer on what you mean by day and night data and how it's classified, especially in the case of posible crossover start and end times. Examples:
Or are you wanting to classify as day or night based on whether the timespan of the data row is more in the day or night period?
Hope that makes sense.
Réponse acceptée
Eric Sofen
le 22 Sep 2022
SunriseSunset = table((1:365)',datetime(2022,1,1:365,6,30,0)', datetime(2022,1,1:365,18,30,0)', VariableNames=["DoY", "Sunrise", "Sunset"]); % let's suppose sunrise and sunset don't change throughout the year
data = timetable(datetime(2022,1,1,0,0:90:10000,0)', rand(112,1), VariableNames="Data");
isday = isbetween(data.Time,SunriseSunset.Sunrise',SunriseSunset.Sunset');
% This takes advantage of implicit expansion and results in a big logical array
% of which particular day period the data's row time corresponds to. Just
% aggregate over rows to get a vector of which times are occuring during
% daytime.
data.isday = sum(isday,2)
4 commentaires
Eric Sofen
le 23 Sep 2022
Presumably those NaTs are in your original data. Either missing timestamps or timestamps in a different format. Datetime can only parse one format at a time.
If you understand where the NaTs come from and just want to get rid of them, take a look at rmmissing.
Plus de réponses (1)
Steven Lord
le 22 Sep 2022
I'm going to use the sunrise and sunset times for Natick, MA for this example. I'll create some random datetime values for the next three days:
fmt = 'MMM dd, h:mm a';
threeDaysInMinutes = minutes(days(3));
data = datetime('today', 'Format', fmt) + minutes(randi([0 threeDaysInMinutes], 10, 1))
Now I'll create the vector of sunset and sunrise values. Elements 1, 3, 5, etc. are sunset values and elements 2, 4, 6, etc. are sunrise values.
sunriseSunset = ["Sep 21, 2022 6:44 pm"; ...
"Sep 22, 2022 6:32 am"; "Sep 22, 2022 6:42 pm"; ...
"Sep 23, 2022 6:33 am"; "Sep 23, 2022 6:40 pm"; ...
"Sep 24, 2022 6:34 am"; "Sep 24, 2022 6:39 pm"; ...
"Sep 25, 2022 6:36 am"; "Sep 25, 2022 6:37 pm"; ...
"Sep 26, 2022 6:37 am"];
bins = datetime(sunriseSunset, 'Format', fmt)
By the way I constructed the sunriseSunset array, anything falling in odd numbered bins (bin 1 being between elements 1 and 2 of sunriseSunset, for example) is considered night and anything falling in even numbered bins is considered day.
whichBin = discretize(data, bins)
Let's look at the results in words rather than odd or even bin numbers. In each row, the value in the Dates variable falls after the value in the Begin variable and before the value in the End variable.
categories = categorical(["Night"; repmat(["Day"; "Night"], 4, 1)]); % Alternate Night and Day
result = table(data, bins(whichBin), bins(whichBin+1), categories(whichBin), ...
'VariableNames', ["Dates", "Begin", "End", "Day or Night"])
0 commentaires
Voir également
Catégories
En savoir plus sur Dates and Time dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!