
% A snippet of code to write a netCDF file.  You need to read
% the time variable and unit information from another file.

% xgrid, ygrid, and level are vectors that are defined elsewhere
% fnin, fnout are filenames for existing and new files, respectively

% var and varname are the variable to be written and its name, respectively

nccreate( fnout, varname, 'Dimensions', { 'lon', length(xgrid), ...
  'lat', length(ygrid), 'level', length(level), 'time', inf } );

nccreate(   fnout, 'lon', 'Dimensions', { 'lon', length(xgrid) } );
ncwrite(    fnout, 'lon', xgrid );
ncwriteatt( fnout, 'lon', 'long_name', 'longitude' );
ncwriteatt( fnout, 'lon', 'units', 'degrees east' );
nccreate(   fnout, 'lat', 'Dimensions', { 'lat', length(ygrid) } );
ncwrite(    fnout, 'lat', ygrid );
ncwriteatt( fnout, 'lat', 'long_name', 'latitude' );
ncwriteatt( fnout, 'lat', 'units', 'degrees north' );

time_units = ncreadatt( fnin, 'time', 'units' );
time = ncread( fnin, 'time' );
nccreate(   fnout, 'time', 'Dimensions', { 'time', inf } );
ncwrite(    fnout, 'time', time );
ncwriteatt( fnout, 'time', 'long_name', 'time' );
ncwriteatt( fnout, 'time', 'units', time_units );
