Main Content

ncwrite

Write data to netCDF file

    Description

    ncwrite(filename,varname,vardata) writes the text or numeric data in vardata to an existing variable varname in the netCDF file filename.

    The ncwrite function writes the data in vardata starting at the beginning of the variable and, if needed, automatically extends the unlimited dimensions. For more information on unlimited dimensions, see the Dimensions argument of the nccreate function.

    example

    ncwrite(filename,varname,vardata,start) writes data to an existing variable, beginning at the location specified by start. Use this syntax to append data to an existing variable or to write partial data.

    example

    ncwrite(filename,varname,vardata,start,stride) writes data with the interval between the indices of each dimension specified by stride.

    example

    Examples

    collapse all

    Create a file myfile.nc containing a variable named c.

    nccreate("myfile.nc","c")

    Write a scalar value to the variable.

    ncwrite("myfile.nc","c",299792458)

    Read and display the variable from the file.

    speedOfLight = ncread("myfile.nc","c")
    speedOfLight = 299792458
    

    Create a file myfile.nc with an empty 3-by-6 numeric variable vmark. To disable the default fill value for missing or empty variables, set the FillValue name-value argument to "disable".

    nccreate("myfile.nc","vmark", ...
             "Dimensions",{"x",3,"y",6},"FillValue","disable")

    Write a 3-by-3 array to the variable, and then read and display vmark from the file. The ncwrite function writes data starting at the beginning of the variable.

    ncwrite("myfile.nc","vmark",3*eye(3))
    varData = ncread("myfile.nc","vmark")
    varData = 3×6
    
         3     0     0     0     0     0
         0     3     0     0     0     0
         0     0     3     0     0     0
    
    

    Add another 3-by-3 array to the variable vmark starting at the fourth column of the first row. The ncwrite function writes the array starting at the location [1 4].

    ncwrite("myfile.nc","vmark",5*eye(3),[1 4])
    varData = ncread("myfile.nc","vmark")
    varData = 3×6
    
         3     0     0     5     0     0
         0     3     0     0     5     0
         0     0     3     0     0     5
    
    

    Create a file myfile.nc with an empty 6-by-6 numeric variable vmark. To disable the default fill value for missing or empty variables, set the FillValue name-value argument to "disable".

    nccreate("myfile.nc","vmark", ...
             "Dimensions", {"x",6,"y",6},"FillValue","disable") 

    Write a 3-by-3 numeric array to the variable vmark starting at the location [1 1] with a spacing of 2 between the variable indices along each dimension.

    ncwrite("myfile.nc","vmark",3*eye(3),[1 1],[2 2])
    varData = ncread("myfile.nc","vmark")
    varData = 6×6
    
         3     0     0     0     0     0
         0     0     0     0     0     0
         0     0     3     0     0     0
         0     0     0     0     0     0
         0     0     0     0     3     0
         0     0     0     0     0     0
    
    

    Input Arguments

    collapse all

    Filename of an existing netCDF file, specified as a string scalar or character vector.

    If the netCDF file does not exist, then use the nccreate function to create it first.

    Example: "myFile.nc"

    Variable name, specified as a string scalar or character vector containing the name of a variable in the netCDF file.

    If filename specifies a file with format netcdf4, you can specify the location of the variable within the group hierarchy by specifying varname as a fully qualified name.

    If the variable does not exist, then use the nccreate function to create it first.

    Example: "myVar"

    Example: "/myGrp/mySubGrp/myNestedVar"

    Variable data, specified as a numeric array or text. The value of vardata must be compatible with data type of the netCDF variable varname.

    If the variable varname has attributes _FillValue, scale_factor, or add_offset, then the ncwrite function expects the data to be compatible with the double data type. To cast vardata into the netCDF data type, the ncwrite function applies these attribute conventions in a sequence before writing vardata:

    1. If the add_offset attribute exists, then ncwrite subtracts the value of the add_offset attribute from the values in vardata.

    2. If the scale_factor attribute exists, then ncwrite divides the values in vardata by the value of the scale_factor attribute.

    3. If the _FillValue attribute exists, then ncwrite replaces NaN vardata values with values equal to that of the _FillValue attribute.

    Note

    For variables of type NC_STRING, vardata can contain UTF-8-encoded characters; for variables of type NC_CHAR, vardata must contain only ASCII-encoded characters.

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | char | string

    Starting location of the data in the variable, specified as a numeric vector of positive integers. For an N-dimensional variable, specify start as a vector of length N containing 1-based indices.

    If you do not specify start, then the ncwrite function starts writing the variable from the first index along each dimension.

    Example: [2 1 3]

    Data Types: double

    Space between the variable indices along each dimension, specified as a numeric vector of integers. For an N-dimensional variable, specify stride as a vector of length N. The elements of the stride vector correspond, in order, to the dimensions of the variable. A value of 1 accesses adjacent values of the netCDF variable in the corresponding dimension, a value of 2 accesses every other value in the corresponding dimension, and so on.

    If you do not specify stride, then the ncwrite function writes the data with a default spacing of 1 along each dimension.

    Example: [2 10 1]

    Data Types: double

    Tips

    • MATLAB® interprets data as column major, but the netCDF C API interprets data as row major. Multidimensional data in the netCDF C API shows dimensions in the reverse of the order shown by MATLAB and consequently appears transposed.

    Version History

    Introduced in R2011a

    expand all