Main Content

ncread

Read data from variable in NetCDF data source

Description

example

vardata= ncread(source,varname)reads all the data from the variablevarnamesource, wheresourceis the NetCDF file, an OPeNDAP NetCDF data source, or the HTTP URL of a remote NetCDF file (with the#mode=bytessuffix for byte-range reading).

example

vardata= ncread(source,varname,start,count)reads data beginning at the location specified instart. Thecountargument specifies the number of elements to read along each dimension.

example

vardata= ncread(source,varname,start,count,stride)returns data with the interval between the indices of each dimension of the variable specified bystride.

Examples

collapse all

Read and plot variable namedpeaksfrom the fileexample.nc.

peaksData = ncread('example.nc','peaks'); whospeaksData
Name Size Bytes Class Attributes peaksData 50x50 5000 int16

情节peaksDataand add a title.

surf(double(peaksData)); title('Peaks Data');

Figure contains an axes object. The axes object with title Peaks Data contains an object of type surface.

Read and plot only a subset of the variable data starting from the location[25 17]until the end of each dimension.

startLoc = [25 17];% Start location along each coordinatecount = [Inf Inf];% Read until the end of each dimensionpeaksData = ncread('example.nc','peaks',startLoc,count); whospeaksData
Name Size Bytes Class Attributes peaksData 26x34 1768 int16

情节the data.

surf(double(peaksData)); title('Peaks Data Starting at [25 17]');

Figure contains an axes object. The axes object with title Peaks Data Starting at [25 17] contains an object of type surface.

Read and plot data, where the data is sampled at a specified spacing between variable indices along each dimension. Start reading from the location instartLocand read variable data at intervals specified instride. A value of1stride, accesses adjacent values in the corresponding dimension. Whereas, a value of2accesses every other value in the corresponding dimension, and so on.

startLoc = [1 1]; count = [10 15]; stride = [2 3]; sampledPeaksData = ncread('example.nc','peaks',startLoc,count,stride); whossampledPeaksData
Name Size Bytes Class Attributes sampledPeaksData 10x15 300 int16

情节the data.

surf(double(sampledPeaksData)); title('Peaks Data Subsampled by [2 3]');

Figure contains an axes object. The axes object with title Peaks Data Subsampled by [2 3] contains an object of type surface.

Input Arguments

collapse all

Source name, specified as a character vector or string scalar containing the name of a NetCDF file, the URL of an OPeNDAP NetCDF data source, or an HTTP URL of a remote NetCDF file for byte-range reading. To enable byte-range reading of remote datasets, append#mode=bytesto the end of the HTTP URL. For details about byte-range reading, consult the NetCDF documentation.

Example:"myNetCDFfile.nc"

Data Types:char|string

Variable name, specified as a character vector or string scalar containing the name of a variable in the NetCDF file or OPeNDAP NetCDF data source.

Data Types:char|string

Starting location, specified as a numeric vector of positive integers. For anN-dimensional variable,startis a vector of lengthNcontaining 1-based indices.

If you do not specifystart,n thencreadfunction starts reading the variable from the first index along each dimension.

Data Types:

Number of elements to read, specified as a numeric vector of positive integers. For anN-dimensional variable,countis a vector of lengthN, specifying the number of elements to read along each dimension. If any element ofcountisInf,nncreadreads until the end of the corresponding dimension.

If you do not specifycount,n thencreadfunction reads the variable data until end of each dimension.

Data Types:

Space between the variable indices along each dimension, specified as a numeric vector of integers. For anN-dimensional variable,strideis vector of lengthN. The elements of thestridevector correspond, in order, to the variable's dimensions. A value of1accesses adjacent values of the NetCDF variable in the corresponding dimension. Whereas, a value of2accesses every other value of the NetCDF variable in the corresponding dimension, and so on.

If you do not specifystride,n thencreadfunction reads the data with a default spacing of1along each dimension.

Data Types:

Output Arguments

collapse all

Variable data, returned as numeric arrays for numeric data types, text for text data types, and cell arrays for user-defined variable length array data types.

In most cases, thencread函数使用MATLAB®data type that is the closest type to the corresponding NetCDF data type.

For numeric data types, when at least one of the variable attributes_FillValue,scale_factor, oradd_offsetis present, thenncreadreturnsvardataof type. In addition,ncreadapplies these conventions:

  • If the_FillValueattribute exists, thenncreadreplacesvardatavalues equal to_FillValuevalues withNaNs. If the_FillValueattribute does not exist, thenncreadqueries the NetCDF library for the fill value of the variable.

  • If thescale_factorattribute exists, thenncreadmultiplies variable data by the value of thescale_factorattribute.

  • If theadd_offsetattribute exists, thenncreadadds the value of theadd_offsetattribute to the variable data.

Note

For variables of typeNC_CHAR,ncreadfunction supports readingvardatathat contains only ASCII-encoded characters. ReadingUTF-8encoded characters is supported from variables of typeNC_STRINGNetCDF-4files.

Data Types:single||在t8|在t16|在t32|在t64|uint8|uint16|uint32|uint64|cell|char|string

Limitations

  • The performance of byte-range reading is slower than reading from other sources.

Version History

Introduced in R2011a