This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Read data from NetCDF variable


data = netcdf.getVar(ncid,varid)
data = netcdf.getVar(ncid,varid,start)
data = netcdf.getVar(ncid,varid,start,count)
data = netcdf.getVar(ncid,varid,start,count,stride)
data = netcdf.getVar(___,output_type)


data = netcdf.getVar(ncid,varid) returns data, the value of the variable specified by varid. MATLAB® attempts to match the class of the output data to NetCDF class of the variable.


For variable values containing text, the netcdf.getVar function supports reading only of data that is ASCII encoded.

ncid is a NetCDF file identifier returned by netcdf.create or

data = netcdf.getVar(ncid,varid,start) returns a single value starting at the specified index, start.

data = netcdf.getVar(ncid,varid,start,count) returns a contiguous section of a variable. start specifies the starting point and count specifies the amount of data to return.

data = netcdf.getVar(ncid,varid,start,count,stride) returns a subset of a section of a variable. start specifies the starting point, count specifies the extent of the section, and stride specifies which values to return.

data = netcdf.getVar(___,output_type) specifies the data type of the return value data. Specify the output data type as one of these character vectors.


This function corresponds to several functions in the NetCDF library C API. To use this function, you should be familiar with the NetCDF programming paradigm. See netcdf for more information.


collapse all

Open the example file,

ncid ='','NC_NOWRITE');

Get the name of the first variable in the file.

varname = netcdf.inqVar(ncid,0)
varname = 

Get variable ID of the first variable, given its name.

varid = netcdf.inqVarID(ncid,varname)
varid = 0

Get the value of the variable. Use the variable ID as the second input to the netcdf.getVar function.

data = netcdf.getVar(ncid,varid)
data = 6.0221e+23

Display the data type of the output value.

whos data
  Name      Size            Bytes  Class     Attributes

  data      1x1                 8  double              

Get the value of the avogadros_number variable again, specifying that the output data type should be single.

data = netcdf.getVar(ncid,varid,'single');

Display the data type of the output value.

whos data
  Name      Size            Bytes  Class     Attributes

  data      1x1                 4  single              

Close the NetCDF file.

Was this topic helpful?