MATLAB: I have a time table with a year’s worth of current measurements. (Time format is Year-Month​-Day-Hour-​Min-Sec) How to take each day and count how many times the current was over a specific reading that day.

dayMATLABretimetimetime tabletimetableyear

My timetable contains the current readings and stardard deviations for the year. I have used retime to find means and standard deviations in the past, but I am having trouble isolating each individual day to calcualtions for each.

Best Answer

The method input argument to retime can be a function handle to a function that accepts a vector of data and returns a scalar or row vector. Try writing a function that accepts the data from each day and returns the number of values that are greater than your threshold and passing that into retime as the method input.
Let's start with some random data.
rng default
mpd = minutes(days(1)); % minutes per day
T = datetime('today');
x = randi(mpd, 100, 1); % Minutes after midnight
y = randi(50, 100, 1);
Create a timetable.
tt = timetable(T + minutes(x), y);
Define a function to count number of y values greater than 20 in each bin.
fh = @(data) nnz(data > 20);
Retime the original timetable using the method fh, binning the data by hour.
tt2 = retime(tt, 'hourly', fh);
Let's see how many hours had at least 1 value in the original data greater than 20. Just show the first couple.
H = head(tt2)
Look at the values in tt that fell into the first bin in tt2.
tt(timerange(H.Time(1), H.Time(2)), :)
Since I set rng default at the top, you should see what I do: three values that exceed 20 and one that doesn't in the time between midnight and 1 AM. So the count of 3 in tt2 for that bin is correct.
ans =
4×1 timetable
Time y
____________________ __
03-Sep-2019 00:52:00 49
03-Sep-2019 00:46:00 46
03-Sep-2019 00:50:00 8
03-Sep-2019 00:18:00 41
Bin 6 (5 AM to 6 AM) of tt has no values of y greater than 20, so the corresponding element in tt2 is 0.
>> tt(timerange(H.Time(6), H.Time(7)), :)
ans =
2×1 timetable
Time y
____________________ _
03-Sep-2019 05:23:00 6
03-Sep-2019 05:51:00 1
>> tt2(6, :)
ans =
timetable
Time y
____________________ _
03-Sep-2019 05:00:00 0