Subset data within time range

2 views (last 30 days)
Lucas campos
Lucas campos on 28 Oct 2020
Answered: Peter Perkins on 18 Nov 2020
Hello,
I have a database of Date, Time and Value. I would like to create a subset of the variable 'Value' every 5 minutes, and extract the max, min and sum of this subset. Any ideias?
tks

Answers (2)

Mathieu NOE
Mathieu NOE on 29 Oct 2020
hello
my 2 cents suggestion
[num,txt,raw] = xlsread('time_data.xlsx') ;
timeday = num(:,1); % time expressed in fraction of a day 24 hours
data = num(:,2);
time_s = timeday*24*3600;
samples = length(time_s);
dt = (time_s(end)-time_s(1))/(samples-1);
% t = (0:dt:(samples-1)*dt); % new time vector (unique values)
Fs = 1/dt;
% main loop : extract min max vales for every 5 min data %
buffer_in_minutes = 5; % put minutes here
buffer_size = floor(buffer_in_minutes*60/dt);
for ci = 1:floor(samples/buffer_size)
ind_start = 1+(ci-1)*buffer_size;
ind_stop = ind_start + buffer_size;
data_tmp = data(ind_start:ind_stop);
min_val(ci) = min(data_tmp);
max_val(ci) = max(data_tmp);
sum_val(ci) = sum(data_tmp);
time_index_s(ci) = mean(time_s(ind_start:ind_stop));
end
% x are you seconds values
s = seconds(time_index_s);
% Change the duration format to hh:mm
s.Format = 'hh:mm:ss';
figure(1);
plot(s,min_val,'+',s,max_val,'+',s,sum_val,'+');grid

Peter Perkins
Peter Perkins on 18 Nov 2020
Your database appears to be a spreadsheet.
Don't use xlsread. Use readtimetable or readtable, depending on what version you have. Yopu will need to read in DATE and TIME as datetimes and durations (or convert them to those types) and add them together. Difficult to say exactly what this entails without knowing what version you have and precisely what is in your spreadsheet. Once you have a timetable, it's a one-liner to get what you want: retime. Three lines, actually, because you need three values.
tt = <some combination of readtable/readtimetable and conversion/addition to end up with a timetable with a var named VALUE>
tt1 = retime(tt,'regular','min','TimeStep',minutes(5));
tt2 = retime(tt,'regular','max','TimeStep',minutes(5));
tt3 = retime(tt,'regular','sum','TimeStep',minutes(5));
tt123 = [tt1 tt2 tt3];

Categories

Find more on Data Type Conversion 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!