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

1 次查看(过去 30 天)
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

采纳的回答

Aquatris
Aquatris 2018-8-3
编辑:Aquatris 2018-8-3
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 个评论
Ayo Deas
Ayo Deas 2018-8-4
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 2018-8-4
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by