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 Center および File ExchangeClimate Science and Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by