Find date time value that corresponds to maximum daily value

19 views (last 30 days)
I have a dataset from a tide gauge that measures tide elevation every 6 minutes for 3 and a half years which adds up to over 200,000 data values. I was able to extract the maximum daily values, but I also want to know the corresponding date time values. So I have a vector of date time values, a vector of tide elevation values of the same length, and a subset of maximum daily tide values, and I want the corresponding date time values to that maximum daily tide subset. Can anyone help me? I can provide my dataset if that helps.

Answers (2)

Peter Perkins
Peter Perkins on 23 Jan 2019
Edited: Peter Perkins on 31 Jan 2019
I'm not sure retime will do all of what's asked for: it only works on one variable at a time. Getting the max tide for each day is easy, getting the specific time during each day when that happened is trickier. You can do it with retime, but I think it would take a few tricks, unless I'm not seeing something. Here's a version that uses a grouped rowfun. It requires one to convert the timetable (temporarily) to a table, because the row times of a timetable cannot curently be specified as an input to the function being applied.
>> X = rand(10,1);
>> tt = timetable(X,'RowTimes',datetime(2019,1,23,4:8:76,0,0))
tt =
10×1 timetable
Time X
____________________ _______
23-Jan-2019 04:00:00 0.95974
23-Jan-2019 12:00:00 0.34039
23-Jan-2019 20:00:00 0.58527
24-Jan-2019 04:00:00 0.22381
24-Jan-2019 12:00:00 0.75127
24-Jan-2019 20:00:00 0.2551
25-Jan-2019 04:00:00 0.50596
25-Jan-2019 12:00:00 0.69908
25-Jan-2019 20:00:00 0.8909
26-Jan-2019 04:00:00 0.95929
>> t = timetable2table(tt);
>> t.Day = dateshift(t.Time,'start','day');
t =
10×3 table
Time X Day
____________________ _______ ____________________
23-Jan-2019 04:00:00 0.95974 23-Jan-2019 00:00:00
23-Jan-2019 12:00:00 0.34039 23-Jan-2019 00:00:00
23-Jan-2019 20:00:00 0.58527 23-Jan-2019 00:00:00
24-Jan-2019 04:00:00 0.22381 24-Jan-2019 00:00:00
24-Jan-2019 12:00:00 0.75127 24-Jan-2019 00:00:00
24-Jan-2019 20:00:00 0.2551 24-Jan-2019 00:00:00
25-Jan-2019 04:00:00 0.50596 25-Jan-2019 00:00:00
25-Jan-2019 12:00:00 0.69908 25-Jan-2019 00:00:00
25-Jan-2019 20:00:00 0.8909 25-Jan-2019 00:00:00
26-Jan-2019 04:00:00 0.95929 26-Jan-2019 00:00:00
>>> rowfun(@myFun,t,'GroupingVariable','Day','OutputVariableNames',{'MaxTime' 'MaxX'})
ans =
4×4 table
Day GroupCount MaxTime MaxX
____________________ __________ ____________________ _______
23-Jan-2019 00:00:00 3 23-Jan-2019 04:00:00 0.95974
24-Jan-2019 00:00:00 3 24-Jan-2019 12:00:00 0.75127
25-Jan-2019 00:00:00 3 25-Jan-2019 20:00:00 0.8909
26-Jan-2019 00:00:00 1 26-Jan-2019 04:00:00 0.95929
And then convert that back to a timetable. I think you can also do the above with splitapply.
myFun looks like this:
function [tmax,xmax] = myFun(t,x)
[xmax,imax] = max(x,[],1);
tmax = t(imax);
  3 Comments
SaaraL
SaaraL on 27 Feb 2021
I am trying to do the same thing, but on many stations at once. Is that possible? Tried to do it on one station, but it gives me an error saying that there are too many input arguments.
Peter Perkins
Peter Perkins on 9 Mar 2021
Add a grouping variable for Station to what I have shown. "By station and day" is no different than "by day", just one more grouping variable.

Sign in to comment.


Star Strider
Star Strider on 11 Jan 2019
It will likely be best to create a timetable if your data, then use the retime function to calculate the maximum daily value. You might find the documentation section on Synchronize and Aggregate Data Values helpful to determine the time the maximum occurred.

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!