Skip to main content

10.3) High Level Access: Writing


Matlab can create and write to NetCDF files using the "nccreate" and "ncwrite" functions.

[matlab]
>> nccreate(filename,varname) % creates an array varname within filename
>> nccreate(filename,varname,options)
>> % options can be to specify dimensions, class, NetCDF file version and fill value
>> % each option is an identifying string followed by a value
[/matlab]

Available options are described in the Matlab documentation. For example:

[matlab]
>> a=rand(360,180); % make some data
>> filename='data.nc';
>> nccreate(filename, 'a', 'Dimensions', {'lon' 360 'lat' 180}, 'Format', 'classic');
>> ncwrite(filename, 'a', a);
>> ncdisp(filename)
[/matlab]

Note that after we write the NetCDF file , it is always a good idea to check that it has been made properly. This is achieved through the "ncdisp" command. We could also have used "ncinfo" to look at the format of the data entries.

Command   Description Example
ncdisp Display text description of the contents ncdisp(filename)
nccreate Create a new file nccreate(file,varname)
nccreate(filename,varname,name,value)
ncwrite Write data to the file ncwrite(filename,varname,vardata)
ncwriteatt Write attribute data to the file ncwriteatt(filename,varname,attname,attvalue)