Index exceeds array bounds and some other mistakes
Show older comments
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 Comments
madhan ravi
on 20 Dec 2018
which line?
KSSV
on 20 Dec 2018
lat{i} = ncread(ncfiles(i).name, 'latitude'); ny = length(lat{i});
time{i} = ncread(ncfiles(i).name, 'time'); nt = length(time{i});
The above lines? There should not be such error actually.
Jonas Damsbo
on 20 Dec 2018
Jan
on 20 Dec 2018
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.
Accepted Answer
More Answers (1)
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 Comment
Jonas Damsbo
on 27 Dec 2018
Edited: Jonas Damsbo
on 27 Dec 2018
Categories
Find more on Creating and Concatenating Matrices 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!