I have multiple excel files like the one shown below (hourly data). I want to obtain the daily average (e.g. from 17:00 to 16:00 of next day).I only know a little Matlab. Currently my solution is below but it got some problem.
- Read each excel file and store the data in a variable.
- Merge all the data in a single variable.
- Find the row number (n) with 17:00. For this, I obtained the datenum of each row and then just search (find) the datenum corresponding to 17:00.
- Average the data between each two rows (n(i)).
It works fine if the raw data is right. But the problems are below.
- Some data at 17:00 are missing. So there will be error when it try to find the row with 17:00.
- When I have more than 100 excel file, it runs very very slow.
Can any one give some suggestion how to solve the problem? I prefer to use Matlab since it is the only tool I know a little. Many thanks!
Edit 1: Code provided
Below is the code to obtain daily average. I have combined all the data in the file "summary_file.xls" (the code of this part is not shown.
As the example table shown below.
- The average for the time between 17:00, 1 Sep. and 2 Sep. has no problem.
- For days with data missing, I would like to obtain the average for all available data before 17:00.
- Example: since the data of 17:00 3 Sep. is missing, the average for 2-3 Sep. cannot be obtained. For such situation, I would like to obtain the average for all data before 17:00 (i.e. untill 9:00 3 Sep.)
- Then for 3-4 Sep., I would like to calculate the average starting from 18:00.
- For days with all data missing, just make all data as 0 or marked as not available.
[num,txt,raw] = xlsread('summary_file.xls'); % read file
date_num = num(:,11); % read the column containing date number
starting = '2003/09/05-17:00'; % starting time
starting = datenum(starting,formatIn_2); % convert starting time to date number
ending = '2003/09/09-17:00';% ending time
ending = datenum(ending,formatIn_2); %convert ending time to date number
s = starting:1:ending; % All date number with 17:00
%% find the row number with 17:00
for i = 1:ending-starting+1
[a(i) b(i)] = find(date_num==s(i));
end
%% Store the averaged data in variable p
for i = 1:ending-starting
p(i,:) = mean(num(a(i):a(i+1)-1,:));
end
Sample input data -
+------+-------+----------+-------+-------+-------+-------+-------+-------+ | 2003 | 1-Sep | 15:00 | 100.2 | 29 | 70.5 | 3.903 | 728 | 0 | +------+-------+----------+-------+-------+-------+-------+-------+-------+ | 2003 | 1-Sep | 16:00 | 100.1 | 29.31 | 70.7 | 4.328 | 611.8 | 0 | | 2003 | 1-Sep | 17:00 | 100.1 | 29.64 | 67.06 | 3.719 | 434.8 | 0 | | 2003 | 1-Sep | 18:00 | 100.1 | 29.67 | 64.4 | 3.005 | 172.4 | 0 | | 2003 | 1-Sep | 19:00 | 100.1 | 29.06 | 68.22 | 2.292 | 19.89 | 0 | | 2003 | 1-Sep | 20:00 | 100.2 | 28.43 | 74.7 | 2.436 | 0.428 | 0 | | 2003 | 1-Sep | 21:00 | 100.2 | 27.92 | 76.2 | 1.931 | 0.006 | 0 | | 2003 | 1-Sep | 22:00 | 100.3 | 27.67 | 77.3 | 1.825 | 0.007 | 0 | | 2003 | 1-Sep | 23:00 | 100.4 | 27.55 | 77.9 | 1.622 | 0.007 | 0 | | 2003 | 1-Sep | 24:00:00 | 100.4 | 27.69 | 77.8 | 0.863 | 0.008 | 0 | | 2003 | 2-Sep | 1:00 | 100.4 | 27.55 | 78.3 | 0.879 | 0.008 | 0 | | 2003 | 2-Sep | 2:00 | 100.3 | 27.05 | 82.1 | 1 | 0.016 | 0.762 | | 2003 | 2-Sep | 3:00 | 100.3 | 26.41 | 86.8 | 0.805 | 0.006 | 0 | | 2003 | 2-Sep | 4:00 | 100.2 | 26.6 | 85.5 | 0.522 | 0.011 | 0.508 | | 2003 | 2-Sep | 5:00 | 100.2 | 25.53 | 83.8 | 2.158 | 0.011 | 0 | | 2003 | 2-Sep | 6:00 | 100.3 | 24.5 | 86.6 | 2.711 | 0.016 | 0 | | 2003 | 2-Sep | 7:00 | 100.4 | 24.85 | 86.9 | 2.562 | 0.016 | 4.318 | | 2003 | 2-Sep | 8:00 | 100.6 | 21.11 | 94 | 8.15 | 9.96 | 26.67 | | 2003 | 2-Sep | 9:00 | 100.6 | 22.23 | 91.9 | 5.065 | 31.67 | 0.254 | | 2003 | 2-Sep | 10:00 | 100.6 | 23.51 | 88.8 | 5.742 | 39.16 | 0.254 | | 2003 | 2-Sep | 11:00 | 100.6 | 24 | 87.7 | 4.494 | 97.8 | 0 | | 2003 | 2-Sep | 12:00 | 100.6 | 24.69 | 85.3 | 4.709 | 142.2 | 0 | | 2003 | 2-Sep | 13:00 | 100.5 | 25.57 | 82.8 | 5.66 | 259.1 | 0 | | 2003 | 2-Sep | 14:00 | 100.4 | 25.69 | 81.9 | 5.634 | 157.5 | 0 | | 2003 | 2-Sep | 15:00 | 100.3 | 26.18 | 79.1 | 5.564 | 308.2 | 0 | | 2003 | 2-Sep | 16:00 | 100.3 | 26.08 | 78.3 | 6.283 | 135.3 | 0 | | 2003 | 2-Sep | 17:00 | 100.3 | 25.75 | 81.2 | 4.595 | 55.68 | 0.762 | | 2003 | 2-Sep | 18:00 | 100.3 | 25.01 | 84.5 | 4.843 | 55.21 | 1.778 | | 2003 | 2-Sep | 19:00 | 100.3 | 25.15 | 86.1 | 1.433 | 22.43 | 0 | | 2003 | 2-Sep | 20:00 | 100.3 | 24.98 | 86.1 | 1.985 | 0.301 | 0 | | 2003 | 2-Sep | 21:00 | 100.3 | 24.75 | 85.1 | 0.712 | 0.009 | 0 | | 2003 | 2-Sep | 22:00 | 100.4 | 24.76 | 85.3 | 1.546 | 0.011 | 0 | | 2003 | 2-Sep | 23:00 | 100.5 | 24.92 | 84.5 | 1.186 | 0.008 | 0 | | 2003 | 2-Sep | 24:00:00 | 100.5 | 24.96 | 84.9 | 1.31 | 0.007 | 0 | | 2003 | 3-Sep | 1:00 | 100.5 | 25 | 85.3 | 0.702 | 0.012 | 0 | | 2003 | 3-Sep | 2:00 | 100.5 | 24.99 | 86 | 0.35 | 0.017 | 0 | | 2003 | 3-Sep | 3:00 | 100.4 | 25.07 | 86.1 | 0.69 | 0.008 | 0 | | 2003 | 3-Sep | 4:00 | 100.3 | 24.92 | 86.5 | 1.347 | 0.011 | 0 | | 2003 | 3-Sep | 5:00 | 100.3 | 25.27 | 85.5 | 0.834 | 0.009 | 0 | | 2003 | 3-Sep | 6:00 | 100.3 | 24.97 | 86.9 | 0.627 | 0.012 | 0 | | 2003 | 3-Sep | 7:00 | 100.3 | 24.8 | 87.7 | 0.755 | 0.108 | 0 | | 2003 | 3-Sep | 8:00 | 100.4 | 25.54 | 85 | 0.202 | 37.11 | 0 | | 2003 | 3-Sep | 9:00 | 100.4 | 26.72 | 81 | 1.853 | 219.4 | 0 | | 2003 | 3-Sep | 18:00 | 100.2 | 29.67 | 56.39 | 2.856 | 456.2 | 0 | | 2003 | 3-Sep | 19:00 | 100.2 | 30.17 | 53.66 | 2.204 | 266 | 0 | +------+-------+----------+-------+-------+-------+-------+-------+-------+