Index exceeds array bounds and some other mistakes
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
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
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
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
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
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!