export in nc in matlab

To export data in NetCDF format in Matlab, you can use the built-in NetCDF tools available in Matlab. Here are the general steps to export data in NetCDF format:

  1. Open the NetCDF file for writing. You can use the netcdf.create function to create a new NetCDF file or netcdf.open function to open an existing NetCDF file for writing.
main.m
ncid = netcdf.create('example.nc', 'NC_WRITE');
48 chars
2 lines
  1. Define the dimensions, variables and attributes of the NetCDF dataset. You can use the netcdf.defDim, netcdf.defVar and netcdf.putAtt functions for that.
main.m
% define dimensions
nx = netcdf.defDim(ncid, 'x', 50);
ny = netcdf.defDim(ncid, 'y', 100);

% define variables
varid = netcdf.defVar(ncid, 'data', 'double', [nx ny]);

% define attributes
netcdf.putAtt(ncid, varid, 'long_name', 'example data');
netcdf.putAtt(ncid, varid, 'units', 'm/s');
289 chars
11 lines
  1. Write the data to the NetCDF file. You can use the netcdf.putVar function to write the data to the NetCDF file.
main.m
% generate data
data = rand(50, 100);

% write data to file
netcdf.putVar(ncid, varid, data);
94 chars
6 lines
  1. Close the NetCDF file. You can use the netcdf.close function to close the NetCDF file.
main.m
netcdf.close(ncid);
20 chars
2 lines

This will create a NetCDF file 'example.nc' with the data variable 'data' and the defined attributes 'long_name' and 'units'.

gistlibby LogSnag