h5read
Read data from HDF5 dataset
Syntax
Description
Examples
Read Entire 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
⋮
Read Subset of HDF5 Dataset
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 HDF5 Dataset with Specified Spacing Between Indices
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
Input Arguments
filename
— Filename
string scalar | character vector
Filename of an existing HDF5 file, specified as a string scalar or character vector.
Depending on the location of your file, filename
can take 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 in
Example:
Example:
| ||||||||
Uniform resource locator (URL) (since R2024b) | If the file is located by an internet URL, then
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:
|
If your file consists of several physical files using the Family driver, specify
filename
using a format specifier. For example, to use the Family driver with the two filesfamily0.h5
andfamily1.h5
, specifyfilename
as"family%d.h5"
.If your file consists of several physical files using the Multi driver or the Split driver, specify
filename
as the leading prefix of the names of the physical files. For example, to use the Multi driver with the six filesmulti-b.h5
,multi-g.h5
,multi-l.h5
,multi-o.h5
,multi-r.h5
, andmulti-s.h5
, specifyfilename
as"multi"
.
ds
— Dataset name
character vector | string scalar
Dataset 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 location
vector of ones (default) | numeric vector
Starting 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 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 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 indices
vector of ones (default) | numeric vector
Space 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, 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.
Limitations
h5read
does not support reading references to groups.MATLAB does not support internet URLs that require authentication.
MATLAB Online™ supports internet URLs associated with Microsoft® OneDrive™ files and folders, while the installed version of MATLAB supports only local OneDrive files.
The
h5read
function issues an error for datasets that fail file integrity checks. To read data in such situations, use theH5P.set_relax_file_integrity_checks
function and the low-level HDF5 interface. (since R2024b)
Version History
Introduced in R2011aR2024b: Datasets that fail file integrity checks no longer supported (R2024b Update)
The h5read
function now issues an error for datasets that fail file
integrity checks. Previously, this function did not issue an error for such datasets. To
read data in such situations, use the H5P.set_relax_file_integrity_checks
function and the low-level HDF5 interface.
This table shows how to update your code to use the low-level HDF5 interface.
R2024a and Earlier | Starting in R2024b |
---|---|
myData = h5read("myFilename.h5","myDatasetName"); |
fapl = H5P.create("H5P_FILE_ACCESS"); H5P.set_relax_file_integrity_checks(fapl,"H5F_RFIC_UNUSUAL_NUM_UNUSED_NUMERIC_BITS") fid = H5F.open("myFilename.h5","H5F_ACC_RDONLY",fapl); dset_id = H5D.open(fid,"myDatasetName"); myData = H5D.read(dset_id); H5D.close(dset_id) H5F.close(fid) H5P.close(fapl) |
R2024b: Read data from HDF5 files over HTTP and HTTPS
You can read data from HDF5 files stored in primary online sources by using the
h5read
function with an internet URL.
R2020b: Read data from HDF5 files at a remote location
You can read data from HDF5 files in remote locations, such as Amazon S3, Windows Azure Blob Storage, and HDFS.
R2020b: Read data from HDF5 files with Unicode names
You can read data from HDF5 files whose names are encoded as Unicode characters.
R2020a: Read nonscalar string data as MATLAB string arrays
h5read
returns HDF5 string arrays as MATLAB string arrays rather than cell arrays of character vectors. Single (scalar)
HDF5 strings are returned as MATLAB character vectors.
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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)