## How to calculate averaged values from 5-min interval values

Asked by Daphne PARLIARI

### Daphne PARLIARI (view profile)

on 19 Sep 2019
Latest activity Commented on by Daphne PARLIARI

### Daphne PARLIARI (view profile)

on 2 Oct 2019 at 7:54
Accepted Answer by David K.

### David K. (view profile)

Hello guys.
I have the following question : I have observations for temperature in 5-minute interval, from 1/1/2015 to 31/1/2015 (8928 rows in total). From these data I must extract the hourly average value for temperature, which means that eventually I must have 24 hourly averaged values.
I would appreciate any ideas on the matter!
PS. I have attached the excel file I'm working on. Please note that the format of time appears diferently on Matlab.

R2016a

### David K. (view profile)

Answer by David K.

### David K. (view profile)

on 19 Sep 2019
Edited by David K.

### David K. (view profile)

on 25 Sep 2019 at 13:20

Using for loops is a very straightforward way that this can be done:
x = yourData; %your data
size1 = length(x)/12;
size2 = 24;
sub1 = ones(size1,1); % intermediate matrix for averaging every hour
for n = 1:size1 % 8928/12 = 744
sub1(n) = mean(x(12*(n-1)+1 : (12*n))); % averages every 12 values to average every hour
end
hourlyVals = ones(size2 , 1); % 744/31 = 24
for n = 1:size2
hourlyVals(n) = mean(sub1(n:24:size1)); % calculate average of each hour across every day.
end
*edited to fix issue brought up in comments*

Daphne PARLIARI

### Daphne PARLIARI (view profile)

on 1 Oct 2019 at 9:11
Thank you, I tried the correction you proposed but the hourly values of temperature display a completely reverse behavior than normal. I attach plotTcorr.tif: maximum T is during night which is completely irrational.
Before the correction of the code, shape of diurnal T was normal but the values were too low. Now the values seem more normal but the shape of the curve is reversed!
Plus if I use:
size2 = 24;
size3=length(RH);
hourlyValsRH = ones(size2 , 1);
for n = 1:size2
hourlyValsRH(n) = mean(RH(n:24:size3)); % calculate average of each hour across every day.
end
the hourlyValsRH table is missing the value at 13.00...
David K.

### David K. (view profile)

on 1 Oct 2019 at 14:21
So I looked at the excel table itself to calculate the mean and it had the same flipped situation. Since it appears that if we only look at the first few days it is as expected then I think it is possible there is something wrong with the data. Your data stops at the 11th hour instead of the full 24 for the last day. perhaps at some point the data got off. I do not know how you would figure it out but I think that is up to you with your data.
To fix the RH I would do the same thing as last time where you just find the places it is missing and fix it manually.
Daphne PARLIARI

### Daphne PARLIARI (view profile)

on 2 Oct 2019 at 7:31
If I delete the last 12 values in order to have all the days with 24 hourly values, then the shape of the diurnal variation of T is correct but the values are too low.
If I add again the last 12 values, the curve loses its shape but the hourly values are correct. It seems that I can't have it both ways.

### Andrei Bobrov (view profile)

Answer by Andrei Bobrov

### Andrei Bobrov (view profile)

on 1 Oct 2019 at 7:39
Edited by Andrei Bobrov

### Andrei Bobrov (view profile)

on 1 Oct 2019 at 7:39

T = readtable('parko_hourly_1_1_2015_eos_16_5_2015_T_RH MINE.xlsx','sheet','Φύλλο1');
T = T(~isnan(T.minute),[1:5,7:8]);
T.YEAR = str2double(T.YEAR);
T{:,{'AIR_TEMP_DegC','RH'}} = str2double(T{:,6:7});
T = T(:,[1:5,8:9]);
T_hour = varfun(@(x)mean(x,'omitnan'),T,'InputVariables',{'AIR_TEMP_DegC','RH'},'GroupingVariables','HOUR');

Daphne PARLIARI

### Daphne PARLIARI (view profile)

on 1 Oct 2019 at 9:55
Thank you!!
I run it and this message appears
"Warning: Variable names were modified to make them valid MATLAB identifiers. "
Andrei Bobrov

### Andrei Bobrov (view profile)

on 2 Oct 2019 at 5:26
Yes! MATLAB modified of names variables in your table.
Daphne PARLIARI

### Daphne PARLIARI (view profile)

on 2 Oct 2019 at 7:54
Your help was crucial! Thank you a million times!