Convert hourly to daily data

42 ビュー (過去 30 日間)
Christos Kaskouras
Christos Kaskouras 2019 年 3 月 13 日
コメント済み: Eshrat Jahan 2022 年 6 月 17 日
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 件のコメント
Guillaume
Guillaume 2019 年 3 月 13 日
More than confusing, the code is not valid matlab syntax.
Christos Kaskouras
Christos Kaskouras 2019 年 3 月 13 日
I am sorry for that. I am kind of "old school" programmer. However, I am loosing just one row
Attached you can finf the file.
Thank you!

サインインしてコメントする。

採用された回答

Andrei Bobrov
Andrei Bobrov 2019 年 3 月 13 日
T = readtable('IT_GR_data.csv','ReadV',1);
T.Properties.VariableNames{1} = 'Date';
TT = table2timetable(T,'RowTime','Date');
TTout = retime(TT,'daily','mean');
  2 件のコメント
Youssef saad
Youssef saad 2020 年 9 月 21 日
i want to covert my data (text) 1 colomn 8760 row from hourly to daily 1 colomn 365 row,
basicaly i want to calculate the daily average of my data to obtain a 1x365 dataset
can you help please help me
Eshrat Jahan
Eshrat Jahan 2022 年 6 月 17 日
@andrei Bobrov can you kindly write the code with respect to older Matlab versions like 2015.

サインインしてコメントする。

その他の回答 (2 件)

Guillaume
Guillaume 2019 年 3 月 13 日
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 件のコメント
Andrei Bobrov
Andrei Bobrov 2019 年 3 月 15 日
+1
Francisco Molteni
Francisco Molteni 2021 年 10 月 3 日
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
Christos Kaskouras 2019 年 3 月 13 日
Solve it.
Thank you guys!

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by