MATLAB: Index exceeds array bounds and some other mistakes

arraysblockingindexinvalidoperator

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

Best Answer

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.