Extrapolate the times of entry and exit in timetable

4 views (last 30 days)
Erica Corradi
Erica Corradi on 3 Jul 2018
Commented: Peter Perkins on 3 Jul 2018
I have a timetable: I want to extrapolate the times of entry and exit from a certain room. I did a for loop on rows and two if loops: the first, I take the column for all the rooms on the i-th row (pir_tab_night.Column1streamId (i)) and I place it == at the room I'm interested in; I also added the condition that the start time must be 0 (in such a way as to take the first line in which the person enters the room) ... here it gives me error (Comparison is not defined between datetime and double arrays). the second if cycle is for the time out of the room: within this cycle I calculate the vect_duration for each (t_end-t_start) and then I go to concatenate with the function cat(1, vect_duration, t_end-t_start). Finally I repositioned the times = 0 to start a new count. This is my code:
vect_duration = []; t_start = 0; t_end = 0; for i = 1:length(pir_tab_night.t) if pir_tab_night.Column1streamId(i) == 'dbuid-17' & t_start == 0 t_start = pir_tab_night.t(i); end if pir_tab_night.Column1streamId(i) ~= 'dbuid-17' & t_start ~= 0 t_end = pir_tab_night.t(i-1); vect_duration = t_end - t_start; vect_duration = cat(1, vect_duration, t_end-t_start); t_start = 0; t_end = 0; end end
Can someone help me? Thank you

Accepted Answer

Paolo
Paolo on 3 Jul 2018
Edited: Paolo on 3 Jul 2018
%Find dbuid-17 in your second column.
dbuid17indx = pir_tab_night.Column1streamId==categorical({'dbuid-17'});
%Use diff function to determine difference between entries.
diffindx = diff(dbuid17indx);
%Convert diffindx to char. Find patterns: 1, variable numbers of 0s, forward slash /.
[startIndx,endIndx] = regexp(char(diffindx+'0')','(10*/)');
%Group all entry and exit times.
times = arrayfun(@(x,y) pir_tab_night.t(x+1:y),startIndx,endIndx,'un',0);
times is a cell array, where
times{1} =
12-Sep-2017 21:38:45
12-Sep-2017 21:39:33
times{2} =
12-Sep-2017 23:23:43
12-Sep-2017 23:24:19
times{3} =
13-Sep-2017 04:37:12
13-Sep-2017 04:39:04
times{4} =
13-Sep-2017 06:26:34
13-Sep-2017 06:26:55
13-Sep-2017 06:27:32
... and so on. times contains the dates corresponding to every "entry" and "exit", as indicated by dbuid-17 in your second column.
  6 Comments
Peter Perkins
Peter Perkins on 3 Jul 2018
This is a great vectorized solution to problem that seems like it would need a loop but really doesn't. A couple of minor readability suggestions:
1) it probably isn't necessary to explicitly construct a categorical value in creating dbuid17indx, I think this
dbuid17indx = pir_tab_night.Column1streamId=='dbuid-17';
should work.
2) Might be more readable to use seconds(3) rather than duration(0,0,3).
3) regexp is pretty challenging. If I'm reading the code correctly, it might be easier to just find positive and negative elements of diffindx.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!