How to find the transition for datetime data?

Hi I have a question about how to find the transition for datetime data. I have a time series data and I want to detect the start transition and end transition. I use this following code:
changingdate = rainanalysis{:,'DatumUhrzeit'} >= 1 ;
% use false as logical vector to determine transition. With function diff,
% transitions from false (0) to true (1) will be 1 and transitions from true
% to false will be -1. This will be 1 at the start of a dry period and -1 after the end
datetransitions = diff([false; changingdate; false]);
datestarts = find(datetransitions == 1);
dateends = find(datetransitions == -1) -1;
However I got an error "Comparison is not defined between datetime and double arrays." I attach also the time series table and the picture contain error message here. Thank you for your help.

 Accepted Answer

changingdate = diff(rainanalysis{:,'DatumUhrzeit'}) >= 1 ;

6 Comments

Thank you for your reply Walter. I try your code above. However, the logical data produced still cant detect the changes, so for example I have :
'19-Oct-2017 08:53:53'
'21-Oct-2017 02:53:30'
'21-Oct-2017 04:20:18'
'21-Oct-2017 04:24:40'
'21-Oct-2017 04:29:14'
'21-Oct-2017 06:18:12'
'21-Oct-2017 06:20:08'
'21-Oct-2017 06:21:42'
'21-Oct-2017 06:23:05'
'21-Oct-2017 06:24:51'
'21-Oct-2017 06:27:20'
'21-Oct-2017 06:34:40'
'22-Oct-2017 01:11:14'
there should be a change from 19 to 21 and also from 21 to 22, however the logical value I got is :
true
false
false
false
false
false
false
false
false
false
false
false
false
I actually want to define the transition and the start of the transition as I will use it for filtering the data. Is it possible for datetime data? Thank you very much for your help.
The code is correct for what you asked of it. It detects when adjacent times differ by more than a day. If you want to know when the calendar date changes, one way is
changingdate = diff( dateshift( rainanalysis.DatumUhrzeit, 'start', 'day') ) >= 1;
You could also risk
changingdate = diff( day(rainanalysis.DatumUhrzeit) ) ~= 0;
This is less robust than the first version because of the possibility that two adjacent dates might be exactly one calendar month apart.
The first code works perfect. Thanks Walter! I am sorry I have one more question. I want to add 0 value in the first row, because the resulted data, due to diff function, has one number reduced. Therefore, the logical data will have the same size as the original data if I put 0 value in the first row of changingdate data. Do you know how to do it? Thank you very much for your help, Walter.
changingdate = [false; diff( day(rainanalysis.DatumUhrzeit) ) ~= 0];
However, the datestarts and dateends code that you have is already correct, and putting the false there would require that the other code be changed.
Walter's solutions are correct, but I might suggest substituting caldays(1) for 1, for readability.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!