Read data from HDF5 dataset
Get the metadata for a dataset from the HDF5 file and then read the dataset.
Display the metadata for a dataset /g4/lat
from the HDF5 file example.h5
.
h5disp('example.h5','/g4/lat')
HDF5 example.h5 Dataset 'lat' Size: 19 MaxSize: 19 Datatype: H5T_IEEE_F64LE (double) ChunkSize: [] Filters: none FillValue: 0.000000 Attributes: 'units': 'degrees_north' 'CLASS': 'DIMENSION_SCALE' 'NAME': 'lat'
Read the dataset.
data = h5read('example.h5','/g4/lat')
data = 19×1
-90
-80
-70
-60
-50
-40
-30
-20
-10
0
⋮
Get the metadata for a dataset from the HDF5 file and then read a subset of the dataset.
Display the metadata for a dataset /g4/world
from the HDF5 file example.h5
.
h5disp('example.h5','/g4/world')
HDF5 example.h5 Dataset 'world' Size: 36x19 MaxSize: 36x19 Datatype: H5T_IEEE_F64LE (double) ChunkSize: [] Filters: none FillValue: 0.000000
Starting from the beginning of data, read a 5-by-3 subset of the data from the dataset.
start = [1 1]; count = [5 3]; data = h5read('example.h5','/g4/world',start,count)
data = 5×3
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
Read data from a dataset, where the data is sampled at a specified spacing between the dataset indices along each dimension.
First, display the metadata for a dataset /g4/lon
from the HDF5 file example.h5
. The variable in the dataset has one dimension with 36 elements.
h5disp('example.h5','/g4/lon')
HDF5 example.h5 Dataset 'lon' Size: 36 MaxSize: 36 Datatype: H5T_IEEE_F64LE (double) ChunkSize: [] Filters: none FillValue: 0.000000 Attributes: 'units': 'degrees_east' 'CLASS': 'DIMENSION_SCALE' 'NAME': 'lon'
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; count = 18; stride = 2; subsetData = h5read('example.h5','/g4/lon',startLoc,count,stride);
Examine the output variable subsetData
.
whos subsetData
Name Size Bytes Class Attributes subsetData 18x1 144 double
filename
— File nameFile name, specified as a character vector or string scalar containing the name of an existing HDF5 file.
Depending on the location of your file, filename
can take on one
of these forms.
Location | Form | ||||||||
---|---|---|---|---|---|---|---|---|---|
Current folder | Specify the name of the file in
Example:
| ||||||||
Other folders | If the file is not in the current folder or in a folder on the
MATLAB® path, then specify the full or relative path name in
Example:
Example:
| ||||||||
Remote Location | If the file is stored at a remote location, then
Based on your remote location,
For more information, see Work with Remote Data. Example:
|
ds
— Dataset nameDataset name, specified as a character vector or string scalar containing the name of the dataset in the HDF5 file. An HDF5 dataset is a multidimensional array of data elements, together with supporting metadata.
start
— Starting locationStarting location, specified as a numeric vector of positive integers. For an
N
-dimensional dataset, start
is a vector of
length N containing 1-based indices. The elements of
start
correspond, in order, to the variable dimensions.
If you do not specify start
, then the h5read
function starts reading the dataset from the first index along each dimension.
count
— Number of elementsInf
's (default) | numeric vectorNumber of elements to read, specified as a numeric vector of positive integers. For
an N
-dimensional dataset, count
is a vector of
length N
, specifying the number of elements to read along each
dimension. The elements of count
correspond, in order, to the
variable dimensions. If any element of count
is
Inf
, then h5read
reads until the end of the
corresponding dimension.
If you do not specify count
, then the h5read
function reads data until the end of each dimension.
stride
— Spacing between indicesSpace between the indices along each dimension of the dataset, specified as a
numeric vector of integers. For an N
-dimensional variable in the
dataset, stride
is a vector of length N
. The
elements of the stride
vector correspond, in order, to the variable
dimensions. A value of 1
accesses adjacent values of the variable in
the corresponding dimension. Whereas, a value of 2
accesses every
other value of the variable in the corresponding dimension, and so on.
If you do not specify stride
, then the h5read
function reads the data with a default spacing of 1
along each
dimension.
h5disp
| h5readatt
| h5write
| h5writeatt
You have a modified version of this example. Do you want to open this example with your edits?