how is the average 10 minutes to hours ?

I have processed the data before, and generated data with name X consisting of 2 columns, column 1 is time and the second column is data. I have data per 10 minutes and I want to change it to hour. I have read the mathwork script but I don't understand it.
I have a script
times2 = x (:,1);
dn1 = datenum(times2);
min_time1 = min(dn1);
min_time_dv1 = datevec(min_time1);
min_time_dv1(5) = floor(min_time_dv1(5) / 60) * 60;
first_slot_dn1 = datenum(min_time_dv1);
max_time1 = max(dn1);
max_time_dv1 = datevec(max_time1);
max_time_dv1(5) = floor(max_time_dv1(5) / 60) * 60;
last_slot_dn1 = datenum(max_time_dv1);
hours_as_days = 1 / (24) ;
slot_dns2 = first_slot_dn1 : hours_as_days : last_slot_dn1;
slot_ds2 = datestr(slot_dns2);
[~, slot_idx2] = histc(dn1, slot_dns2);
mean_y = accumarray(slot_idx2, x(:,2), [length(slot_dns2)-1,1], @mean, nan);
N = [cellstr(slot_ds2(1:end-1,:)), num2cell(mean_y)];
I have tried to get 10 minutes of data with this script and it worked, but for hours it didn't work,
and produce
Error using accumarray
Second input VAL must be full numeric, logical, or char vector or scalar.
can someone correct my script? thank you for the help of someone who will help me in solving my problem. I will accept your advice.

Risposte (1)

The simplest way to achieve what you want is to forget about all these outdated tools such as datenum, histc, etc. and convert your x into a timetable. I'm not sure what form takes your time in x, but it should be something like:
t_x = timetable(datetime(x(:, 1), x(:, 2)));
Once that is done, it is then trivial to calculate the 10 minutes, hourly, daily, whatever period mean, in just one line, with retime:
t_10mins = retime(t_x, 'regular', 'mean', 'TimeStep', minutes(10)); %average every 10 minutes
t_hourly = retime(t_x, 'hourly', 'mean')

1 Commento

thank you for helping me, but i use matlab 2014a and there is no function (retime, timestep, timetable). is there any other way ?

Accedi per commentare.

Richiesto:

il 30 Apr 2019

Commentato:

il 30 Apr 2019

Community Treasure Hunt

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

Start Hunting!

Translated by