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".