MATLAB Answers

Extracting and plotting max/min values from timeseries

39 views (last 30 days)
Lindazeta
Lindazeta on 12 May 2020
Commented: Lindazeta on 13 May 2020
I have a time series with continuous measurements of pH. The measurements are every 5 minutes, and the timeseries is a total of 13 days.
I want to find and plot the max and min value for each day, where the max and min values are based on 24 observations of a mean pH/hour.
So far I've been able to get the mean pH per hour, but I don't know how to find the max/min values from these and then plot as a continuous time series.
This is what I've done so far to find the mean pH for every hour:
dnv = [time];
dv6 = datevec(dnv); % Date vector matrix, 6 columns
dn4 = datenum([dv6(:,1:4) repmat([0 0],size(dv6,1),1)]);
[Du,~,di] = unique(dn4, 'stable');
Means = accumarray(di, pH, [], @mean); % Creating means for every hour
Out = [datevec(Du) Means]; % Output separated columns, date time mean
de = datetime(Out(:,1:6)); % Merged date and time back into 1 column
So, how do I find the max and min values with the associated date and time for plotting?

  0 Comments

Sign in to comment.

Accepted Answer

Peng Li
Peng Li on 12 May 2020
Better take advantage of table format and splitapply function.
tbl = readtable('data.csv');
tbl.day = day(tbl.Date);
tbl.hour = hour(tbl.Date);
[Group, hourTbl] = findgroups(tbl(:, {'day', 'hour'}));
hourTbl.mean = splitapply(@nanmean, tbl.pH, Group);
[Group, dayTbl] = findgroups(hourTbl(:, 'day'));
dayTbl.max = splitapply(@max, hourTbl.mean, Group);
dayTbl.min = splitapply(@min, hourTbl.mean, Group);

  4 Comments

Show 1 older comment
Peng Li
Peng Li on 12 May 2020
Modified a bit to show max pH and the corresponding max time. It's a bit twisted but it's working.
tbl = readtable('data.csv');
tbl.year = year(tbl.Date);
tbl.month = month(tbl.Date);
tbl.day = day(tbl.Date);
tbl.hour = hour(tbl.Date);
[Group, hourTbl] = findgroups(tbl(:, {'year', 'month', 'day', 'hour'}));
hourTbl.mean = splitapply(@nanmean, tbl.pH, Group);
[Group, dayTbl] = findgroups(hourTbl(:, {'year', 'month', 'day'}));
temp = splitapply(@(x, y) mymax(x, y), hourTbl.mean, hourTbl.hour, Group);
dayTbl.maxHour = temp(:, 2);
dayTbl.maxpH = temp(:, 1);
dayTbl.time = datetime(dayTbl.year, dayTbl.month, dayTbl.day, dayTbl.maxHour, 0, 0);
function out = mymax(ph, time)
[m, ind] = max(ph);
out = [m, time(ind)];
end
Lindazeta
Lindazeta on 13 May 2020
Awesome! Thanks! One last question though... How do I add the min values? I tried re-writing it but my matlab skills are not great...
Lindazeta
Lindazeta on 13 May 2020
Oh never mind, I made it work! Thank you so much for your help!

Sign in to comment.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!