To my understanding, you have written a MATLAB code to get the daily average temperatures from five stations for the month of June. However, your code does not calculate the average for the last day and throws an “index out of bound” error.
Upon investigating the code, I found that the error occurs due to accessing the “B1(ii+1,1)” element (denoting the start of next day) when “ii” has reached 30, hence it throws “index out of bound” error.
To fix this issue, for the last day of the month, we can use “end” notation to denote the end of data in the cell array, instead of accessing the start of next day in the following way:
Matriz_24h{jj}(ii, 8:9) =nanmean(Matrices{jj}(B1(ii):end, 8:9));
Below is the complete MATLAB code that addresses this task:
for jj = 1:length(Matrices)
numDays = length(M_24h);
for ii = 1:numDays
if ii < numDays
% For all days except the last one
Matriz_24h{jj,1}(ii,8:9) = nanmean(Matrices{jj,1}(B1(ii,1):B1(ii+1,1)-1, 8:9));
else
% For the last day, ensure you go to the end of the data
Matriz_24h{jj,1}(ii,8:9) = nanmean(Matrices{jj,1}(B1(ii,1):end, 8:9));
end
end
end
Please find attached the documentation of “array indexing” in MATLAB for reference:
I trust this will help to resolve the issue.