Index exceeds matrix dimensions is the error I'm getting.
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Aquatris
on 3 Aug 2018
Edited: Aquatris
on 3 Aug 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 Comments
Aquatris
on 4 Aug 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.
More Answers (0)
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!