ncread - Read data from variable in netCDF data source - MATLAB (original) (raw)
Read data from variable in netCDF data source
Syntax
Description
[vardata](#mw%5F560bd1d3-2f9b-43ab-b80a-ca774a3213e5) = ncread([source](#bsu%5Fi8a-1%5Fsep%5Fmw%5Fe339e1a9-285c-4606-9ca3-5d590980a4d2),[varname](#bsu%5Fi8a-1%5Fsep%5Fmw%5F936cf078-b38a-48b9-bb9f-aff8ed8f14fc))
reads all the data from the netCDF variable varname
contained insource
. The returned variable value vardata
is of the MATLABĀ® data type that best matches the netCDF data type ofvarname
.
[vardata](#mw%5F560bd1d3-2f9b-43ab-b80a-ca774a3213e5) = ncread([source](#bsu%5Fi8a-1%5Fsep%5Fmw%5Fe339e1a9-285c-4606-9ca3-5d590980a4d2),[varname](#bsu%5Fi8a-1%5Fsep%5Fmw%5F936cf078-b38a-48b9-bb9f-aff8ed8f14fc),[start](#mw%5Fc40440f3-d75e-421a-a505-30f047a4054a),[count](#mw%5F5d90db5a-9e5c-49a9-a145-a94ee23fd848))
reads data beginning at the location specified by start
. Thecount
argument specifies the number of elements to read along each dimension.
[vardata](#mw%5F560bd1d3-2f9b-43ab-b80a-ca774a3213e5) = ncread([source](#bsu%5Fi8a-1%5Fsep%5Fmw%5Fe339e1a9-285c-4606-9ca3-5d590980a4d2),[varname](#bsu%5Fi8a-1%5Fsep%5Fmw%5F936cf078-b38a-48b9-bb9f-aff8ed8f14fc),[start](#mw%5Fc40440f3-d75e-421a-a505-30f047a4054a),[count](#mw%5F5d90db5a-9e5c-49a9-a145-a94ee23fd848),[stride](#mw%5Fd9c1dcb3-b394-4d44-af97-8bbcb933ebde))
returns data with the interval specified by stride
between the indices of each dimension of the variable.
Examples
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 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 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
Name of the netCDF data source, specified as a string scalar or character vector. Thesource
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 reading
Note
Byte-range reading is slower than other methods for reading from remote sources. For more details about byte-range reading, see the netCDF documentation.
Example: "myNetCDFfile.nc"
Example: "http://_`hostname`_/_`netcdffilename`_#mode=bytes"
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 formatnetcdf4
, 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"
Starting location of the data in the variable, specified as a numeric vector of positive integers. For an _N
_-dimensional variable, specifystart
as a vector of length N
containing 1-based indices.
If you do not specify start
, then thencread
function starts reading the variable from the first index along each dimension.
Example: [2 1 3]
Data Types: double
Number of elements to read, specified as a numeric vector of positive integers orInf
values. For an N
-dimensional variable, specify count
as a vector of lengthN
, containing the number of elements to read along each dimension. If any element of count
is Inf
, thenncread
reads until the end of the corresponding dimension.
If you do not specify count
, then thencread
function reads the variable data until the end of each dimension.
Example: [Inf 10 50]
Data Types: double
Space between the variable indices along each dimension, specified as a numeric vector of integers. For an _N
_-dimensional variable, specifystride
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 thencread
function reads the data with a default spacing of1
along each dimension.
Example: [2 10 1]
Data Types: double
Output Arguments
Variable data, returned as a numeric array, text, or cell array of the MATLAB data type that best matches the netCDF data type ofvarname
. 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
, oradd_offset
is present, vardata
is of typedouble
. Additionally, ncread
applies these attribute conventions in a sequence before returningvardata
:
- 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
- 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 typeNC_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
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 formatnetcdf4
.
Tips
- MATLAB interprets data as column major, but the netCDF C API interprets 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 R2011a
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.
You can read variable length array data types (NC_VLEN
) from netCDF-4 files.
You can read NC_STRING
data from netCDF-4 files.