Main Content

Import HDF4 Files Using Low-Level Functions

Read data from a scientific dataset in an HDF4 file, using the functions in the matlat.io.hdf4.sd namespace. In HDF4 terminology, the numeric arrays stored in HDF4 files are called datasets.

Add Namespace to Import List

Add the matlab.io.hdf4.* path to the import list.

import matlab.io.hdf4.*

Subsequent calls to functions in the matlat.io.hdf4.sd namespace need only be prefixed with sd, rather than the entire namespace path.

Open HDF4 File

Open the example HDF4 file, sd.hdf, and specify read access, using the matlab.io.hdf4.sd.start function. This function corresponds to the SD API routine, SDstart.

sdID = sd.start("sd.hdf","read");

sd.start returns an HDF4 SD file identifier, sdID.

Get Information About HDF4 File

Get the number of datasets and global attributes in the file, using the matlab.io.hdf4.sd.fileInfo function. This function corresponds to the SD API routine, SDfileinfo.

[ndatasets,ngatts] = sd.fileInfo(sdID)
ndatasets = 
4
ngatts = 
1

The file, sd.hdf, contains four datasets and one global attribute.

Get Attributes from HDF4 File

Get the contents of the first global attribute. HDF4 uses zero-based indexing, so an index value of 0 specifies the first index.

HDF4 files can optionally include information, called attributes, that describes the data contained in the file. Attributes associated with an entire HDF4 file are global attributes. Attributes associated with a dataset are local attributes.

attr = sd.readAttr(sdID,0)
attr = 
'02-Sep-2010 11:13:16'

Select Datasets to Import

Determine the index number of the dataset named temperature. Then, get the identifier of that dataset.

idx = sd.nameToIndex(sdID,"temperature");
sdsID = sd.select(sdID,idx);

sd.select returns an HDF4 SD dataset identifier, sdsID.

Get Information About Dataset

Get information about the dataset identified by sdsID using the matlab.io.hdf4.sd.getInfo function. This function corresponds to the SD API routine, SDgetinfo.

[name,dims,datatype,nattrs] = sd.getInfo(sdsID)
name = 
'temperature'
dims = 1×2

    20    10

datatype = 
'double'
nattrs = 
11

sd.getInfo returns information about the name, size, data type, and number of attributes of the dataset.

Read Entire Dataset

Read the entire contents of the dataset specified by the dataset identifier, sdsID.

data = sd.readData(sdsID);

Read Portion of Dataset

Read a 2-by-4 portion of the dataset, starting from the first column in the second row. Use the matlab.io.hdf4.sd.readData function, which corresponds to the SD API routine, SDreaddata. The start input is a vector of index values specifying the location in the dataset where you want to start reading data. The count input is a vector specifying the number of elements to read along each dataset dimension.

start = [0 1];
count = [2 4];
data2 = sd.readData(sdsID,start,count)
data2 = 2×4

    21    41    61    81
    22    42    62    82

Close HDF4 Dataset

Close access to the dataset, using the matlab.io.hdf4.sd.endAccess function. This function corresponds to the SD API routine, SDendaccess. You must close access to all the datasets in and HDF4 file before closing the file.

sd.endAccess(sdsID)

Close HDF4 File

Close the HDF4 file using the matlab.io.hdf4.sd.close function. This function corresponds to the SD API routine, SDend.

sd.close(sdID)

See Also

| | | | |

Related Topics