ncread
Read data from variable in NetCDF data source
Syntax
Description
Examples
Read NetCDF Data
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 Portion of Data from Variable
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 Data with Specified Spacing Between Variable Indices
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
source
— Source name
character vector | string scalar
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=bytes
to
the end of the HTTP URL. For details about
byte-range reading, consult the NetCDF
documentation.
Example: "myNetCDFfile.nc"
Data Types: char
| string
varname
— Variable name
character vector | string scalar
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
start
— Starting location
vector of ones (default) | numeric vector
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
count
— Number of elements
vector of Inf
's (default) | numeric vector
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
stride
— Space between variable indices
vector of ones (default) | numeric vector
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
vardata
— Variable data
numeric | text | cell array
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, the ncread
function uses the 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
, or
add_offset
is present, then ncread
returns
vardata
of type double
. In addition,
ncread
applies these conventions:
If the
_FillValue
attribute exists, thenncread
replacesvardata
values equal to_FillValue
values withNaN
s. If the_FillValue
attribute does not exist, thenncread
queries the NetCDF library for the fill value of the variable.If the
scale_factor
attribute exists, thenncread
multiplies variable data by the value of thescale_factor
attribute.If the
add_offset
attribute exists, thenncread
adds the value of theadd_offset
attribute to the variable data.
Note
For variables of type NC_CHAR
, the ncread
function supports reading vardata
that contains only ASCII-encoded
characters. Reading UTF-8
encoded characters is supported from
variables of type NC_STRING
in NetCDF-4
files.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| cell
| char
| string
Limitations
The performance of byte-range reading is slower than reading from other sources.
Version History
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)