Effacer les filtres
Effacer les filtres

Difference between Events in a Limited Time Domain

2 vues (au cours des 30 derniers jours)
Kostandin Golemi
Kostandin Golemi le 9 Oct 2023
Looking at this specific data set, I would like to limit the time analysis to working days only, that are from Monday to Friday between 6am and 10pm. My goal is to calculate the difference between two consecutive events. However, a problem arises with events that extend beyond time limits. For example, if an event starts at 21:50 on Monday and the next event starts at 6:50 on Tuesday, the real difference between them is 9 hours but I would like to find a way to calculate this difference so that it falls within the domain specified, resulting in a value of 1 hour. Attached another example.
  1 commentaire
Mathieu NOE
Mathieu NOE le 10 Oct 2023
would be nice to share your data and piece of code

Connectez-vous pour commenter.

Réponses (1)

Githin George
Githin George le 16 Oct 2023
Hi Kostandin Golemi,
I understand you have data in a timetable format and would like to find the time difference between every entry in terms of business hours’. You could use the ‘rowfun’ MATLAB function and add a custom logic to resolve the issue as shown below,
% Add 2 columns to the timetable TT - column1 should represent values from
% next row and column2 should be an exact copy of the DateTime Column of the TT
TT.TimeProxy = [TT.Time(2:end); TT.Time(end)]
TT.TimeActual = TT.time
% Use rowfun function to apply your algorithm for finding actual business
% hour on each row
result = rowfun(@yourLogic,TT,"InputVariables",["TimeProxy","TimeActual"])
I’ve provided a template for yourLogic’ function which should contain the algorithm to find actual business hours.
function result = yourLogic(TimeProxy,TimeActual)
% Case 1 - TimeProxy and TimeActual is of Same day
% Direct Calculation
result = hours(TimeProxy-TimeActual);
% Case 2 - TimeProxy and TimeActual is of different Day
% Step 1 - Find duration from 'TimeActual' to End of Day
% Step 2 - while day(TimeProxy) != day(nextBusinessDay)
% Add duration of one full working day
% The following utility function 'busdate' provides
% date of next business day
% Busday = busdate('2023-10-20', 1)
% x = datestr(Busday)
% Step 3 - Add duration from Start of Day to 'TimeProxy'
result = hours(resultStep1 + resultStep2 +_resultStep3);
end
In ‘Case 2’ the busdate function from MATLAB can help in finding next calendar business day.
To know more about ‘rowfun’ and ‘busdate functions please refer to the documentation links below,
I hope this helps.

Catégories

En savoir plus sur Data Type Identification 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!

Translated by