Index exceeds matrix dimensions is the error I'm getting.

1 visualización (últimos 30 días)
Ayo Deas
Ayo Deas el 2 de Ag. de 2018
Comentada: Aquatris el 4 de Ag. de 2018
The error is showing in the Tair_monthly loop. How can I fix this error to display the data?
clear
clc
%Load treering data only file. SiteNo. specises column removed
loc=xlsread('Tree_Ring_Data_AK.xlsx');
year=loc(:,4); % RWI-year change to fit your sheet format
lat=loc(:,2);%latitude
lon=loc(:,1);%longitude
%Function that translate lat and lon into coordination counts
[lonf,latf]=TransLatLon(lon,lat);
files=dir('*.nc');%load Temperature files
for y=1:length(files);%Number of years in your climate file
temp=ncread(files(y).name,'Tair_monthly'); % variable name in climate file
ind=find(year==y+1900);
lont=lonf(ind);
latt=latf(ind);
%
for i=1:length(latt);
for j=1:12;
Tair_monthly(ind(i),j)=temp(lont(i),latt(i),j);
end
end
end
ind=find(year<1901);
for j=1:12
Tair_monthly(ind,j)=NaN;
end

Respuesta aceptada

Aquatris
Aquatris el 3 de Ag. de 2018
Editada: Aquatris el 3 de Ag. de 2018
The reason is simple; since year and Tair_monthly variable do not have sam number of elements, the ind variable tries to access rows of Tair_monthly that do not exist.
Your "year" variable is your raw data. Then you create a variable that has less elements compared to "year" variable called "latt". Then you create the rows of your "Tair_monthly" variable using the reduced number. However at the end you try to reach the indeces that are coming from the "year" variable, which do not exist in the "Tair_monthly" variable.
  4 comentarios
Ayo Deas
Ayo Deas el 4 de Ag. de 2018
Thank you for the suggestion. I adjusted the line for my last loop however I'm still getting the same error for the nested loop which in this case is line 19. The nested loop should produce an array with 12 columns. Any suggestions on how to adjust this loop?
Aquatris
Aquatris el 4 de Ag. de 2018
You need to provide your data for me to further help.
But one thing you can check is if your "temp" variable is a 2D or 3D matrix. I do not have the data so I cannot determine the actual cause.
The reason for the error is basically your variable has 5 elements and you are trying to reach 6. Just use debug tool, stop the program at the error, check what these values are (lont(i),latt(i),j), check the size of your temp etc. and from there you can determine the cause.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by