ncread
Read data from variable in netCDF data source
Syntax
Description
Examples
Read NetCDF Variable
Read and plot a variable named peaks
from the netCDF 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(peaksData)
title("Peaks Data")
Read Portion of Data from Variable
Read and plot only a subset of the peaks
variable data starting from the location [25 17]
until the end of each dimension.
start = [25 17]; % Start location along each coordinate count = [Inf Inf]; % Read until the end of each dimension peaksData = ncread("example.nc","peaks",start,count); whos peaksData
Name Size Bytes Class Attributes peaksData 26x34 1768 int16
Plot the data.
surf(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. Read variable data at intervals specified in stride
, starting from the location in start
. A value of 1
in stride
accesses adjacent values in the corresponding dimension, a value of 2
accesses every other value in the corresponding dimension, and so on.
start = [1 1]; count = [10 15]; stride = [2 3]; sampledPeaksData = ncread("example.nc","peaks",start,count,stride); whos sampledPeaksData
Name Size Bytes Class Attributes sampledPeaksData 10x15 300 int16
Plot the data.
surf(sampledPeaksData)
title("Peaks Data Subsampled by [2 3]")
Input Arguments
source
— Name of netCDF data source
string scalar | character vector
Name of the netCDF data source, specified as a string scalar or character vector. The
source
argument can be one of these values:
The path of a local netCDF source
The OPeNDAP URL of a remote OPeNDAP netCDF data source
The HTTP URL of a remote netCDF source, with
#mode=bytes
appended to the end of the URL to enable byte-range readingNote
Byte-range reading is slower than reading from other sources. For more details about byte-range reading, see the netCDF documentation.
Example: "myNetCDFfile.nc"
Example: "http://
host_name
/netcdf_filename
#mode=bytes"
varname
— Variable name
string scalar | character vector
Variable name, specified as a string scalar or character vector containing the name of a variable in the netCDF data source.
If source
specifies a file with format
netcdf4
, you can specify the location of the variable within the
group hierarchy by specifying varname
as a fully qualified
name.
Example: "myVar"
Example: "/myGrp/mySubGrp/myNestedVar"
start
— Starting location of data in variable
vector of ones (default) | numeric vector
Starting location of the data in the variable, specified as a numeric vector of
positive integers. For an N
-dimensional variable, specify
start
as 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.
Example: [2 1 3]
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 or
Inf
values. For an N
-dimensional
variable, specify count
as a vector of length
N
, containing 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 the end of each
dimension.
Example: [Inf 10 50]
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, specify
stride
as a vector of length N
. The
elements of the stride
vector correspond, in order, to the
dimensions of the variable. A value of 1
accesses adjacent values of
the netCDF variable in the corresponding dimension, a value of 2
accesses every other value 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.
Example: [2 10 1]
Data Types: double
Output Arguments
vardata
— Variable data
numeric array | text | cell array
Variable data, returned as a numeric array, text, or cell array of the MATLAB data type that best matches the netCDF data type of
varname
. For more information about how MATLAB determines the best match, see NetCDF to MATLAB Data Type Conversion.
For numeric variables, when at least one of the attributes
_FillValue
, scale_factor
, or
add_offset
is present, vardata
is of type
double
. Additionally, ncread
applies these
attribute conventions in a sequence before returning
vardata
:
If the
_FillValue
attribute exists:If
vardata
is of typedouble
orsingle
, thenncread
replacesvardata
values equal to that of the_FillValue
attribute withNaN
values.If
vardata
is of any other numeric type, thenncread
replacesNaN
vardata
values as well asvardata
values equal to that of the_FillValue
attribute with0
values.
If the
scale_factor
attribute exists, thenncread
multiplies the values invardata
by the value of thescale_factor
attribute.If the
add_offset
attribute exists, thenncread
adds the value of theadd_offset
attribute to the values invardata
.
Note
If varname
is of type NC_STRING
, then it
can contain UTF-8-encoded characters; if varname
is of type
NC_CHAR
, then it must contain only ASCII-encoded
characters.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| cell
| char
| string
More About
NetCDF to MATLAB Data Type Conversion
The netCDF-related MATLAB functions automatically choose the MATLAB data type that best matches the netCDF data type according to this table.
NetCDF Data Type | MATLAB Data Type |
---|---|
NC_DOUBLE | double |
NC_FLOAT | single |
NC_INT | int32 |
NC_SHORT | int16 |
NC_BYTE | int8 |
NC_CHAR | char |
NC_STRING (*) | string |
NC_INT64 (*) | int64 |
NC_UINT64 (*) | uint64 |
NC_UINT (*) | uint32 |
NC_USHORT (*) | uint16 |
NC_UBYTE (*) | uint8 |
User-defined NC_VLEN types (*) | cell |
(*) These netCDF data types are available only for files with format
netcdf4
.
Tips
MATLAB interprets multidimensional data as column-major, but the netCDF C API interprets multidimensional 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.
Version History
Introduced in R2011aR2022a: Byte-range reading of remote datasets
You can use ncread
for read-only access to remote datasets using
the HTTP byte-range capability, provided that the remote server supports byte-range
access.
R2022a: Read variable length array data types (NC_VLEN
)
You can read variable length array data types (NC_VLEN
) from netCDF-4
files.
R2021b: Read NC_STRING
data
You can read NC_STRING
data from netCDF-4 files.
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 bat365 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)