Error: Index exceeds array bounds

2 次查看(过去 30 天)
Hi
I have tried this question before but now the question will be more simplyfied. Sorry for the last!
I have used a for loop to read 9 ncfiles with the same variables but different timesteps (different months. I have not take this part to this question.
%Using cell arrays for my variables
lat = cell(Nfiles, 1);
lon = cell(Nfiles, 1);
time = cell(Nfiles, 1);
z = cell(Nfiles, 1);
%Read my variables from all files
for i = 1:Nfiles
lon{i} = ncread(ncfiles(i).name, 'longitude'); %9x1 cell [360x1 single]...
lat{i} = ncread(ncfiles(i).name, 'latitude'); %9x1 cell [181x1 single]...
time{i} = ncread(ncfiles(i).name, 'time'); %9x1 cell [124x1 int32]... (6 times) [112x1 int 32]... (3 times)
z{i} = ncread(ncfiles(i).name, 'z'); %9x1 cell [360x181x124 int32]... (6 times) [360x181x112x int 32]... (3 times)
end
%Define the lengths (all longitude, all latitudes and all timesteps
nx = length(lon{i}); %nx = 360
ny = length(lat{i}); %ny = 181
nt = length(vertcat(time{:})); %nt = 1080
%Now I want the data for all the timesteps. Not only for the last one like in the i-loop. So I make a for loop all over the timesteps.
for n = 1:nt
z = ncread(ncfiles(i).name,'z',[1 1 n],[nx ny 1]);
zx(:,1:ny) = z(:,ny:-1:1);
zmean = zmean + zx;
end
When I run the code I got the error:
Index exceeds array bounds.
Error in Blocking (line 38)
z = ncread(ncfiles(i).name,'z',[1 1 n],[nx ny 1]);
Please, somone there can help me?

采纳的回答

per isakson
per isakson 2018-12-27
编辑:per isakson 2019-1-2
There isn't enough information to spot with any certainty what the cause of the error is.
The Matlab error message doesn't tell the bounds of which array is exeeded. I guess it's ncfiles. Had it been inside netcdf the error message would have looked something like (R2018b)
Error using netcdflib
The NetCDF library encountered an error during execution of 'getVarsShort' function - 'Index exceeds dimension bound (NC_EINVALCOORDS)'.
Error in netcdf.getVar (line 140)
data = netcdflib(funcstr,ncid,varid,varargin{:});
Error in internal.matlab.imagesci.nc/read (line 635)
data = netcdf.getVar(gid, varid, ...
Error in ncread (line 66)
vardata = ncObj.read(varName, varargin{:});
Your comment
% Now I want the data for all the timesteps. Not only for the last one like in
% the i-loop. So I make a for loop all over the timesteps.
At the end I miss "for the last one of the nc-files". However, I think it's good practise not to refer to a loop-counter outside the loop in question. In your case i outside the "i-loop".

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by