ncread

Read data from variable in NetCDF data source

Description

example

vardata = ncread(source,varname) reads all the data from the variable varname contained in the NetCDF file or an OPeNDAP NetCDF data source specified by source.

example

vardata = ncread(source,varname,start,count) reads data beginning at the location specified in start. The count argument 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 by stride.

Examples

collapse all

Read and plot variable named peaks from the file example.nc.

peaksData  = ncread('example.nc','peaks');
whos peaksData
  Name            Size            Bytes  Class    Attributes

  peaksData      50x50             5000  int16              

Plot peaksData and add a title.

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

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 coordinate
count  = [Inf Inf]; % Read until the end of each dimension
peaksData  = ncread('example.nc','peaks',startLoc,count);
whos peaksData
  Name            Size            Bytes  Class    Attributes

  peaksData      26x34             1768  int16              

Plot the data.

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

Read and plot data, where the data is sampled at a specified spacing between variable indices along each dimension. Start reading from the location in startLoc and read variable data at intervals specified in stride. A value of 1 in stride, accesses adjacent values in the corresponding dimension. Whereas, a value of 2 accesses 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);
whos sampledPeaksData
  Name                   Size            Bytes  Class    Attributes

  sampledPeaksData      10x15              300  int16              

Plot the data.

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

Input Arguments

collapse all

Source name, specified as a character vector or string scalar containing the name of a NetCDF file or the URL of an OPeNDAP NetCDF data source.

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 an N-dimensional variable, start is a vector of length N containing 1-based indices.

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

Data Types: double

Number of elements to read, specified as a numeric vector of positive integers. For an N-dimensional variable, count is a vector of length N, specifying the number of elements to read along each dimension. If any element of count is Inf, then ncread reads until the end of the corresponding dimension.

If you do not specify count, then the ncread function reads the variable data until end of each dimension.

Data Types: double

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

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

Data Types: double

Output Arguments

collapse all

Variable data, returned as text or numeric arrays.

In most cases, the ncread function uses the MATLAB® datatype that is the closest type to the corresponding NetCDF datatype.

When at least one of the variable attributes _FillValue, scale_factor, or add_offset is present, then ncread returns vardata of type double. In addition, ncread applies these conventions:

  • If the _FillValue attribute exists, then ncread replaces vardata values equal to _FillValue values with NaNs. If the_FillValue attribute does not exist, then ncread queries the NetCDF library for the variable's fill value.

  • If the scale_factor attribute exists, then ncread multiplies variable data by the value of the scale_factor attribute.

  • If the add_offset attribute exists, then ncread adds the value of the add_offset attribute to the variable data.

Note

For variable data containing text, the ncread function supports reading only of vardata that is ASCII encoded.

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

Introduced in R2011a