% Example of how to write a slice at a time for a netcdf variable % name of the netcdf file filename = 'test.nc'; % number of elements in longitude and latitude dimensions imax = 10; jmax = 10; % time origin (for units) torigin = datenum(2000,1,1); % the number of elements for unlimited dimensions and has the special value inf % The dimension can also have a fixed size. % Use the additional option 'Format','64bit' to create a netcdf file compatible % with the library version 3.6 nccreate(filename,'temperature','Dimensions',{'lon',imax,'lat',jmax,'time',inf}); % the dimensions time was already created in the previous call. Its size % does not have to be repeated here. nccreate(filename,'time','Dimensions',{'time'}); % format 31 is 'yyyy-mm-dd HH:MM:SS' ncwriteatt(filename,'time','units',['days since ' datestr(torigin,31)]); % loop over slices for i = 1:31 % create some random data (or load it from somewhere) data = randn(imax,jmax); % assume we have daily data for January 2014 time = datenum(2014,1,1) + i-1; % [1 1 i] are the start indices where to write the data start = [1 1 i]; % write a slice of data ncwrite(filename,'temperature',data,[1 1 i]); % write the corresponding time ncwrite(filename,'time',time - torigin,i); end disp('You can check the file with the command "ncdump -v time -t test.nc".')