MATLAB: How to fix “Attempted to access PETTotalKe​mptcatch(1​09,:); index out of bounds because size(PETTo​talKemptca​tch)=[108,​5]” error

index out of bounds

Hello, I'm trying to fix the mentioned error in this simple loop. I have a matrix( PETTotalKemptcatch) of 108×5 (Rows: monthly time series from January 2008 to December 2016 and columns: 5 different methods for calculating PET). I need to create a new matrix for extracting January, february, march and so on. Each month matrix will have a size(9,5) cause the data is for 9 years for example PETJanuary(9,5). So, in this case, i will have 9 january, 9 february and so on. The following code is just for January.
The code is giving the matrix well but at the same time is giving me this error: Attempted to access PETTotalKemptcatch(109,:); index out of bounds because size(PETTotalKemptcatch)=[108,5]" . I have tried many ways to fix but i havent been succesful in that.
Thanks
p=1;
for j=1:size(PETTotalKemptcatch,1)
PETJanuary(p,:)=PETTotalKemptcatch(12*j-11,:);
p=p+1;
end

Best Answer

It clearly states that you have 108 rows but you‘re trying to access 109th row ? Isn’t that clear?