Main Content

Import HDF4 Files Using Low-Level Functions

This example shows how to read data from a Scientific Data Set in an HDF4 file, using the functions in the matlat.io.hdf4.sd package. In HDF4 terminology, the numeric arrays stored in HDF4 files are called data sets.

Add Package 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 package need only be prefixed with sd, rather than the entire package 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 data sets 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 data sets 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 that the file contains. Attributes associated with an entire HDF4 file are global attributes. Attributes associated with a data set are local attributes.

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

Select Data Sets to Import

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

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

sd.select returns an HDF4 SD data set identifier, sdsID.

Get Information About Data Set

Get information about the data set 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 data set.

Read Entire Data Set

Read the entire contents of the data set specified by the data set identifier, sdsID.

data = sd.readData(sdsID);

Read Portion of Data Set

Read a 2-by-4 portion of the data set, 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 data set where you want to start reading data. The count input is a vector specifying the number of elements to read along each data set 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 Data Set

Close access to the data set, using the matlab.io.hdf4.sd.endAccess function. This function corresponds to the SD API routine, SDendaccess. You must close access to all the data sets 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