MATLAB: Error netcdf.getVar if reading several netcdf files in a row


I am using RMatlab2018a and reading data from netcdf files stored on my computer. I open (and close) the same set of files a certain number of times in a loop. After the n-th iteration (e.g. after 5 iterations), I get the following error:
"Error using netcdflib The NetCDF library encountered an error during execution of 'getVarFloat' function – 'Invalid argument (22)'. Error in netcdf.getVar (line 136) data = netcdflib(funcstr,ncid,varid,varargin{:});"
If I restart Matlab and run exactly the same iteration again, it works.
The data is read as:
ncid =, 'NC_NOWRITE');
varidchx=netcdf.inqVarID(ncid,'chx'); % for the x coordinate called chx
datachx= netcdf.getVar(ncid,varidchx);
Any help greatly appreciated

Best Answer

  • You need not to use such complex functions about ncread, ncinfo. YOu can just use ncread to read the variable you want.