Main Content

netcdf.putVar

Write data to netCDF variable

    Description

    netcdf.putVar(ncid,varid,vardata) writes vardata to the netCDF variable identified by varid in the netCDF file or group specified by ncid.

    example

    netcdf.putVar(ncid,varid,start,vardata) writes a single data value into the variable at the index specified by start.

    netcdf.putVar(ncid,varid,start,count,vardata) writes a section of values into the variable beginning at the index specified by start to the extent specified by count, along each dimension of the variable.

    example

    netcdf.putVar(ncid,varid,start,count,stride,vardata) writes a subsection of values specified by the sampling interval stride.

    Examples

    collapse all

    Create and open a netCDF file.

    ncid = netcdf.create("myfile.nc","NOCLOBBER");

    Define a new dimension. Then associate a new variable with this dimension.

    dimid = netcdf.defDim(ncid,"my_dim",5);
    varid = netcdf.defVar(ncid,"my_var","NC_DOUBLE",dimid);
    netcdf.endDef(ncid)

    Write data to the variable.

    vardata = linspace(0,5,5);
    netcdf.putVar(ncid,varid,vardata)

    Check that the data was written as expected, using the netcdf.inqVar and netcdf.getVar functions.

    [varname,xtype,dimid,natts] = netcdf.inqVar(ncid,varid)
    varname = 
    'my_var'
    
    xtype = 
    6
    
    dimid = 
    0
    
    natts = 
    0
    
    netcdf.getVar(ncid,varid)
    ans = 5×1
    
             0
        1.2500
        2.5000
        3.7500
        5.0000
    
    

    Close the netCDF file.

    netcdf.close(ncid)

    Make a copy of the example netCDF file and open the copy for writing. Write data to the first 10 entries of the temperature variable.

    copyfile("example.nc","myfile.nc")
    ncid = netcdf.open("myfile.nc","NC_WRITE");
    varid = netcdf.inqVarID(ncid,"temperature");
    
    start = 0;
    count = 10;
    vardata = 100:2:118;
    netcdf.putVar(ncid,varid,start,count,vardata);

    Check that the values were written as expected, using the netcdf.getVar function.

    read_data = netcdf.getVar(ncid,varid);
    read_data(1:10)
    ans = 10x1 int16 column vector
    
       100
       102
       104
       106
       108
       110
       112
       114
       116
       118
    
    

    Close the netCDF file.

    netcdf.close(ncid)

    Create and open a netCDF-4 file.

    ncid = netcdf.create("myfile.nc","NETCDF4");

    Define three new dimensions. Then associate a new variable with these dimensions.

    dim1id = netcdf.defDim(ncid,"my_dim1",4);
    dim2id = netcdf.defDim(ncid,"my_dim2",3);
    dim3id = netcdf.defDim(ncid,"my_dim3",2);
    varid = netcdf.defVar(ncid,"my_var","NC_DOUBLE",[dim1id dim2id dim3id]);
    netcdf.endDef(ncid)

    Write one-dimensional data to the three-dimensional variable my_var.

    vardata = 1:24;
    netcdf.putVar(ncid,varid,vardata)

    Check that the data was written as a 4-by-3-by-2 array in column-major order, using the netcdf.getVar function.

    netcdf.getVar(ncid,varid)
    ans = 
    ans(:,:,1) =
    
         1     5     9
         2     6    10
         3     7    11
         4     8    12
    
    
    ans(:,:,2) =
    
        13    17    21
        14    18    22
        15    19    23
        16    20    24
    
    

    Close the netCDF file.

    netcdf.close(ncid)

    Input Arguments

    collapse all

    Identifier of a netCDF source, specified as a nonnegative integer scalar. The netCDF source can be a netCDF file or a netCDF group.

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

    Identifier of a netCDF variable, specified as a nonnegative integer scalar.

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

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

    If you specify a value for count, then the number of entries in vardata must equal the product of the entries of count. Otherwise, the number of entries in vardata must equal the product of the sizes of the dimensions of the variable identified by varid.

    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.

    Note

    • If vardata has more than one dimension, then the netcdf.putVar function flattens vardata in column-major order before writing the data. If the variable identified by varid has more than one dimension, then vardata is written to this variable in column-major order.

    • For variables of type NC_VLEN, if vardata contains any entries that have more than one dimension, then the netcdf.putVar function flattens those entries in column-major order before writing the values. For example, for a variable of type NC_VLEN, specifying vardata as

      {[0.5 0.3] 1.2; [0.4; 0.5] [0 -0.7 5.2; 4.6 2.5 1.8]}

      and specifying vardata as

      {[0.5; 0.3] 1.2; [0.4; 0.5] [0; 4.6; -0.7; 2.5; 5.2; 1.8]}

      have the same effect.

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

    Index from which to start writing data in variable, specified as a numeric vector of positive integers. For an N-dimensional variable, specify start as a vector of length N containing 0-based indices.

    If you do not specify start, then the netcdf.putVar function starts writing data from the initial index along each dimension.

    Example: [1 0 2]

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

    Number of elements to write, specified as a numeric vector of positive integers. For an N-dimensional variable, specify count as a vector of length N containing the number of elements to write along each dimension.

    Example: [1 10 50]

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

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

    If you do not specify stride, then the netcdf.putVar function writes 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.

    • This function corresponds to several (nc_put_var*) functions in the netCDF library C API.

      To use this function, you must be familiar with the netCDF C interface. You can access the netCDF documentation at the netCDF website.

    Version History

    Introduced in R2008b

    expand all