MATLAB: How to calculate Daily mean and monthly mean from hourly data

meanmonthly mean

I have hourly data for 5years continuasly and I would like to calculate daily mean and montly mean.
Sample data file attached

Best Answer

I would recommend the following steps:
  1. Import the data file
  2. Arrange the data and create timetable variable
  3. Apply retime funciton to obtain daily/monthly average
The following is an example:
% Read the data file
opts = detectImportOptions('test_pog.txt');
T = readtable('test_pog.txt',opts);
% Make a datetime vector
Time = datetime(T{:,1},'InputFormat','yyyy.MM.dd.');
Time.Hour = T{:,2};
% Add the datetime vector and remove the 1st&2nd columns
T.Time = Time;
T(:,1:2) = [];
% Convert to timetable
TT = table2timetable(T);
% Apply retime function to obtain daily/monthly mean
TTdailyMean = retime(TT,'daily','mean');
TTmonthlyMean = retime(TT,'monthly','mean');