%% Write Data to NetCDF File Using Low-Level Functions
% This example shows how to use low-level functions to write data to a NetCDF file. The MATLAB® low-level functions provide access to the routines in the NetCDF C library. MATLAB groups the functions into a package, called netcdf. To call one of the functions in the package, you must prefix the function name with the package name.
% To use the MATLAB NetCDF functions effectively, you should be familiar with the information about the NetCDF C Interface.
% To run this example, you must have write permission in your current folder.
% Create a 1-by-50 variable of numeric values named my_data in the MATLAB workspace. The vector is of class double.

my_data = linspace(0,49,50);

%
% Create a NetCDF file named my_file.nc, using the netcdf.create function. The NOCLOBBER parameter is a NetCDF file access constant that indicates that you do not want to overwrite an existing file with the same name.

ncid = netcdf.create('my_file.nc','NOCLOBBER');

% netcdf.create returns a file identifier, ncid. When you create a NetCDF file, the file opens in define mode. You must be in define mode to define dimensions and variables.
%
% Define a dimension in the file, using the netcdf.defDim function. This function corresponds to the nc_def_dim function in the NetCDF library C API. You must define dimensions in the file before you can define variables and write data to the file. In this case, define a dimension named my_dim with length 50.

dimid = netcdf.defDim(ncid,'my_dim',50)

% netcdf.defDim returns a dimension identifier that corresponds to the new dimension. Identifiers are zero-based indexes.
%
% Define a variable named my_var on the dimension, using the netcdf.defVar function. This function corresponds to the nc_def_var function in the NetCDF library C API. Specify the NetCDF data type of the variable, in this case, NC_BYTE.

varid = netcdf.defVar(ncid,'my_var','NC_BYTE',dimid)

% netcdf.defVar returns a variable identifier that corresponds to my_var.
%
% Take the NetCDF file out of define mode. To write data to a file, you must be in data mode.

netcdf.endDef(ncid)

%
% Write the data from the MATLAB workspace into the variable in the NetCDF file, using the netcdf.putVar function. The data in the workspace is of class double but the variable in the NetCDF file is of type NC_BYTE. The MATLAB NetCDF functions automatically do the conversion.

netcdf.putVar(ncid,varid,my_data)

%
% Close the file, using the netcdf.close function.

netcdf.close(ncid)

%
% Verify that the data was written to the file by opening the file and reading the data from the variable into a new variable in the MATLAB workspace.

ncid2 = netcdf.open('my_file.nc','NC_NOWRITE');
x = netcdf.getVar(ncid2,0);

%
% View the data type of x.

whos x

% MATLAB stores data in column-major order while the NetCDF C API uses row-major order. x represents the data stored in the NetCDF file and is therefore 50-by-1 even though the original vector in the MATLAB workspace, my_data, is 1-by-50. Because you stored the data in the NetCDF file as NC_BYTE, MATLAB reads the data from the variable into the workspace as class int8.
%
% Close the file.

netcdf.close(ncid2)

% Copyright 2015 The MathWorks, Inc.