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

조회 수: 1 (최근 30일)
Ayo Deas
Ayo Deas 2018년 8월 2일
댓글: Aquatris 2018년 8월 4일
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