Index exceeds array bounds and some other mistakes

조회 수: 2 (최근 30일)
Jonas Damsbo
Jonas Damsbo 2018년 12월 20일
편집: Jonas Damsbo 2018년 12월 27일
Hi
I have some problems with my code and really need help to finding the error. I stared at me blindly.
Anyone who will take a look at it? The error is in my second for loop in the second and third line.
I got the errors "Indes exceeds array bounds" and "Invalid use of a operator."
%Indlæser alle filer fra folderen
ncfiles = dir('*.nc') ;
Nfiles = length(ncfiles) ;
for i = 1:Nfiles;
%Viser indholdet af filen
ncdisp(ncfiles(i).name) ;
%Åbner filen så den kun kan åbnes
ncid=netcdf.open(ncfiles(i).name,'NOWRITE');
%Indlæser dimensioner, variabler, attributter, unlim
%[ndim, nvar, natt, unlim]=netcdf.inq(ncid);
netcdf.close(ncid);
end
lat = cell(Nfiles, 1);
lon = cell(Nfiles, 1);
time = cell(Nfiles, 1);
z = cell(Nfiles, 1);
for i = 1:Nfiles
lon{i} = ncread(ncfiles(i).name, 'longitude'); nx = length(lon{i});
lat{i} = ncread(ncfiles(i).name, 'latitude'); ny = length(lat{i});
time{i} = ncread(ncfiles(i).name, 'time'); nt = length(time{i});
z{i} = ncread(ncfiles(i).name, 'z'); nz = length(z{i});
end
%Midler geopotentialet til et månedsmiddel
zmean = zeros([nx ny]);
blocks = zeros([nx]);
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;
%pcolor(lon,lat,z');
%shading interp
%drawnow
GHGS = (zx(:,[151+[-1 0 1]])-zx(:,[131+[-1 0 1]]))/20;
GHGN = (zx(:,[171+[-1 0 1]])-zx(:,[151+[-1 0 1]]))/20;
for i=1:ny
blocks(i)=blocks(i)+1;
if GHGS > 0;
disp('The point is blocked')
elseif GHGN < -10;
disp('The point is blocked')
end
end
end
  댓글 수: 4
Jonas Damsbo
Jonas Damsbo 2018년 12월 20일
Oops, I mean the third loop:
z = ncread(ncfiles(i).name,'z',[1 1 n],[nx ny 1]);
zx(:,1:ny) = z(:,ny:-1:1);
Jan
Jan 2018년 12월 20일
In the 2nd for loop, you have 4 lines like:
lon{i} = ncread(ncfiles(i).name, 'longitude'); nx = length(lon{i});
Use one command per line only to support Matlab's JIT acceleration.
The 2nd command is useless, because you overwrite nx in each iteration without using it. So better run it once after the loop.

댓글을 달려면 로그인하십시오.

채택된 답변

Omer Yasin Birey
Omer Yasin Birey 2018년 12월 20일
I believe you meant third for loop's second and third line. Because, in second for loop I don't think these types of errors would occur.
zx(:,1:ny) = z(:,ny:-1:1);
zmean = zmean + zx;
Meanwhile, for those lines try to pre-allocate zx. If you haven't done already.
  댓글 수: 3
Omer Yasin Birey
Omer Yasin Birey 2018년 12월 20일
편집: Omer Yasin Birey 2018년 12월 20일
for n = 1:nt
z = ncread(ncfiles(i).name,'z',[1 1 n],[nx ny 1]);
In this line you use 'i' as index, when your loop's index is 'n'. And because of the last loop, 'i' is at it's maximum which is nFiles. Firstly, you should change it to n.
Guillaume
Guillaume 2018년 12월 20일
" Firstly, you should change it to n."
No, that would certainly not solve the problem. In fact If you do that and length{time{end}) is greater than length(ncfiles) you can be certain that you'll get an index exceeds array dimensions error.
I don't really understand why the OP accepted this answer since it actually doesn't solve anything. Probably a case of accepting the first answer given regardless of whether or not it solves the problem.
As I pointed out in my answer, the most likely reason for some of the problems is that the n loop should be inside the i loop.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Guillaume
Guillaume 2018년 12월 20일
편집: Guillaume 2018년 12월 20일
When complaining about errors always give us the full text of the error message, everything in red, so we don't have to guess from your explanation what the actual error is and on what line it occurs.
Saying that, your code is clearly broken. We have:
for i = 1:Nfiles %i used to increment over ncfiles
...
nt = length(time{i}); %nt not used in the loop
end
So, nt is overwritten at each step of the loop. When the loop finishes, nt is basically
nt = length(time{end});
and all the other intermediate values have been completely forgotten. Clearly, whatever you intended to do, it's not that.
Additionally, after the i loop has completed you still use i:
for n = 1:nt
z = ncread(ncfile(i).name, ... %using i after the end of the i loop
end
At that point, i will always have the value Nfiles.
Possibly, you meant for the n loop to be inside the for loop. It's hard to know since there are no comments explaining what the code is supposed to be doing.
Note that if you can't figure out the problems just by looking at the code you should be using the debugger to step your through program one line at time and see what actually happens instead of what you assume should happen.
edit: Just saw another problem: You're reading z twice. Once as z{i} in the i loop, another time as simply z in the n loop:
z{i} = ncread(ncfiles(i).name, 'z'); %in the i loop
z = ncread(ncfiles(i).name,'z',[1 1 n],[nx ny 1]); %in the n loop
Clearly, it's pointless to do the same thing twice. And clearly, you haven't really thought about what you're doing properly.
  댓글 수: 1
Jonas Damsbo
Jonas Damsbo 2018년 12월 27일
편집: Jonas Damsbo 2018년 12월 27일
I got a question to my code line:
for n = 1:nt
z = ncread(ncfiles(i).name,'z',[1 1 n],[nx ny 1]);
...
Here I use the syntax for ncread:
vardata = ncread(source,varname,start,count)
I got the error "Index exceeds array bounds." Where nx and ny is longitudes and latitudes:
nx = 360
ny = 181
The red error:
Index exceeds array bounds.
Error in Blocking (line 39)
z = ncread(ncfiles(i).name,'z',[1 1 1],[nx ny 1]);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by