MATLAB: Data not written correctly to netcdf file

netcdf

Hello,
I am trying to create a netcdf file with variables 'latitude', 'longitude' and 'zeros' to create a map of the world. I want to create a 0.5 degree grid so my latitude and longitude vectors have lengths of 360 and 720, respectively. My code (below) runs fine but when it creates the variable 'zeros' it does not write any data (i.e. has a cell value of 9.9692100e+36) for rows 714 (columns 246-360) and 715-720 (all columns). This produces a stripe going across my map when I visualise the data. Do you have any idea why this might be happening?
ncid = netcdf.create('Annual.nc','NC_NOCLOBBER');
%Define dimensions
londimid = netcdf.defDim(ncid,'lon', 720);
latdimid = netcdf.defDim(ncid,'lat', 360);
%Define variables
latvarid=netcdf.defVar(ncid, 'lat', 'float', latdimid);
lonvarid=netcdf.defVar(ncid, 'lon', 'float', londimid);
zerosvarid=netcdf.defVar(ncid, 'zeros', 'float', [latdimid londimid]);
netcdf.endDef(ncid)
%Write data to variables
netcdf.putVar(ncid,lonvarid,longitude);
netcdf.putVar(ncid,latvarid,latitude);
netcdf.putVar(ncid, zerosvarid, zeros(360,720));

Best Answer

Anna, the data might not get fully written unless you close the file. A call to close would flush out all the data to the file:
netcdf.close(ncid);
Of course, you would then have to use netcdf.open to open the file again for reading.
Aside, if you have the latest version, you could look at functions like nccreate, ncwrite and ncread which might make it easier to read and write netcdf data.