Convert hourly to daily data
Show older comments
Hi all,
I have some data in an hourly basis and I want to convert them to daily (each day to be the average of 24 rows) .
I have created the following loop but I am missing one value. For the specific period I should be receiving 1461 rows (days) but I am receiving 1460.
Can someone please help me?
t=1;
k=1; step = 24;
for i - step:step:length(x)-step;
if i > step; a = x(i-step:i); else a = x(k:i); end;
AverageDailyData(k,t) = mean(a)q
k=k+1;
end
3 Comments
Raghunandan V
on 13 Mar 2019
The code you have written is very confusing. i think it can be done in much simpler way. Can you share the data?. I can look at the data and then write the code
Guillaume
on 13 Mar 2019
More than confusing, the code is not valid matlab syntax.
Christos Kaskouras
on 13 Mar 2019
Accepted Answer
More Answers (2)
Guillaume
on 13 Mar 2019
Because of the typos and the lack of comments, it's hard to tell how your data is stored. The simplest way to achieve what you want is to store your data in a timetable (the creation of which is probably just one line), then retime that timetable:
hourlydata = timetable(yourdatetimecolumn, yourdatacolumn); %or some other way of creating a timetable
dailydata = retime(hourlydata, 'daily', 'mean')
%all done
This is probably the most reliable and clearest way to do it. It will work even if you're missing some hours, some days are not complete, or if your data is not ordered chronologically. I would strongly recommend that you use this method.
As you seem to imply that your data is ordered chronologically and that you've got the full hourly data for each day, you could just average in groups of 24 rows. You shouldn't use a loop for that:
hourlydata = rand(72, 1); %demo data, supposed to represent 3 days of hourly data
dailydata = mean(reshape(hourlydata, 24, [])).' %rearrange the hourly data in daily columns and take the mean of the 24 hours of each column.
4 Comments
Christos Kaskouras
on 13 Mar 2019
Guillaume
on 13 Mar 2019
Well, with your text file then that's trivially loaded into a timetable as shown by Andrei. You don't even have to worry about renaming the datetime column. You then apply retime as I've shown above:
hourlydata = table2timetable(readtable('IT_GR'));
dailydata = retime(hourlydata, 'daily', 'mean')
Andrei Bobrov
on 15 Mar 2019
+1
Francisco Molteni
on 3 Oct 2021
but when you have gaps in your data the timetable can not remove that and you will have errors, how you can solve that ?
710724 NaN
710725 NaN
710726 NaN
710727 NaN
710728 NaN
710729 NaN
710730 NaN
710731 NaN
710732 NaN
710733 NaN
710734 NaN
710735 NaN
710736 68.5000000000000
710737 68.7222222222222
710738 69.1805555555556
710739 69.1111111111111
710740 69.5833333333333
710741 69.0694444444444
710742 72.9722222222222
710743 71.6527777777778
710744 73.1666666666666
710745 74.2638888888889
710746 77.0972222222222
710747 75.9444444444444
710748 74.1250000000000
710749 73.9861111111111
710750 73.5555555555556
710751 70.0694444444444
710752 68.6666666666666
710753 69.7361111111111
710754 73.1527777777778
710755 75.9305555555556
710756 76.4583333333333
710757 77.3194444444444
710758 78.3888888888889
710759 80.4027777777778
710760 80.1111111111111
710761 80.7916666666667
710762 83.6805555555556
710763 79.9027777777778
710764 78.9444444444444
710765 77.7916666666667
710766 77.2500000000000
Christos Kaskouras
on 13 Mar 2019
0 votes
Categories
Find more on Characters and Strings 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!