Retime Yearly Maximum - Finding Corresponding Dates from Timetable
12 views (last 30 days)
Show older comments
I'm using the retime function to find yearly maximum values for a timetable (e.g., x=timetable(Datetime, Var); YearlyMax = retime(x,'yearly','max')).
I need an efficient means of finding the corresponding date/time from the timetable for each maximum value rather than the 01-Jan-YYYY resulting timetable. Any ideas on the best way of finding the dates for each yearly maximum value with the timetable?
0 Comments
Accepted Answer
Star Strider
on 23 Apr 2023
Try something like this —
Date = datetime(2020,01,01)+caldays(0:3.9*365.25).';
Temp = 80*rand(size(Date))-40;
Humd = randi([1 100],size(Date));
Wx = timetable(Date,Temp,Humd) % Original 'timetable'
WxYr = retime(Wx, 'yearly','max') % Yearly Maxima
Yrs = year(WxYr.Date);
for k = 1:numel(Yrs)
Lv1 = year(Wx.Date) == Yrs(k); % Logical Vector Selecting Days For Selected Year
WxCurYr = Wx(Lv1,:); % Current Year 'timetable'
Lv2 = ismember(WxCurYr.Temp, WxYr(k,:).Temp); % Select Variable To Compare (Here: Temperature) & Return Logical Vector Of Matches
ResultTT{k,:} = WxCurYr(Lv2,:); % Save 'timetable' Arrays
Result(k,:) = timetable2table(WxCurYr(Lv2,:)); % Save Rows In The 'Result' Table
end
Result % Table Of Days Matching Yearly Maximum Temperature
If there is more than one matching value, this becomes slightly more complicated. In that instance, it will be necessary to either return all of the matching days (this usually requires that ‘result’ becomes a cell array, causing further complications later in the code), or use find with ‘Lv2’ and use an element of that vector to return only one of them (the approach I have characteristically taken in these situations, simply because it’s easier).
.
0 Comments
More Answers (1)
Eric Sofen
on 24 Apr 2023
This example gives another way to get the time when the max occurred. See the findMax function and how it's used in rowfun. The difference is you're grouping by year, rather than by site.
0 Comments
See Also
Categories
Find more on Dates and Time 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!