Programming Previous page   Next Page

Using the HDF4 Command-Line Interface

This section describes how to use MATLAB functions to access the HDF4 Application Programming Interfaces (APIs). These APIs are libraries of C routines that you can use to import data from an HDF4 file. For a complete list of the HDF APIs supported by MATLAB and the functions you use to access each one, see the hdf reference page.

Topics covered include

Understanding the HDF4 to MATLAB Syntax Mapping

Each HDF4 API includes many individual routines that you use to read data from files, write data to files, and perform other related functions. For example, the HDF Scientific Data (SD) API includes separate C routines to open (SDopen), close (SDend), and read data (SDreaddata).

Instead of supporting each routine in the HDF APIs, MATLAB provides a single function that serves as a gateway to all the routines in a particular HDF API. For example, the HDF Scientific Data (SD) API includes the C routine SDend to close an HDF file:

To call this routine from MATLAB, use the MATLAB function associated with the SD API, hdfsd. You must specify the name of the routine, minus the API acronym, as the first argument and pass any other required arguments to the routine in the order they are expected. For example,

Handling HDF Routines with Output Arguments.   Some HDF API routines use output arguments to return data. Because MATLAB does not support output arguments, you must specify these arguments as return values.

For example, the SDfileinfo routine returns data about an HDF file in two output arguments, ndatasets and nglobal_atts:

To call this routine from MATLAB, change the output arguments into return values:

Specify the return values in the same order as they appear as output arguments. The function status return value is always specified as the last return value.

Example: Using the HDF4 SD API to Import Data

To illustrate using HDF4 API routines in MATLAB, this section describes how to import HDF4 Scientific Data (SD) into the MATLAB workspace. The following gives an overview of the steps required by the SD API to import data from and HDF file. The following sections walk you through a detailed example.

Step 1: Opening the HDF4 File.   To import an HDF SD data set, you must first open the file using the SD API routine SDstart. In MATLAB, you use the hdfsd function, specifying as arguments:

For example, this code opens the file mydata.hdf for read access:

If SDstart can find and open the file specified, it returns an HDF SD file identifier, named sd_id in the example. Otherwise, it returns -1.

Step 2: Retrieving Information About the HDF File.   To get information about an HDF4 file, you must use the SD API routine SDfileinfo. This function returns the number of data sets in the file and the number of global attributes in the file, if any. (For more information about global attributes, see Exporting MATLAB Data to an HDF4 File.) In MATLAB, you use the hdfsd function, specifying the following arguments:

In this example, the HDF4 file contains three data sets and one global attribute.

Step 3: Retrieving Attributes from an HDF File (Optional).   HDF files can optionally include information, called attributes, that describes the data the file contains. Attributes associated with an entire HDF file are called global attributes. Attributes associated with a data set are called local attributes. (You can also associate attributes with files or dimensions. For more information, see Step 4: Writing Metadata to an HDF File.)

To retrieve attributes from an HDF file, use the HDF4 API routine SDreadattr. In MATLAB, use the hdfsd function, specifying as arguments:

For example, this code returns the contents of the first global attribute, which is the character string my global attribute:

Step 4: Selecting the Data Sets to Import.   To select a data set, use the SD API routine SDselect. In MATLAB, you use the hdfsd function, specifying as arguments:

If SDselect finds the specified data set in the file, it returns an HDF SD data set identifier, called sds_id in the example. If it cannot find the data set, it returns -1.

Step 5: Getting Information About a Data Set.   To read a data set, you must get information about the data set, such as its name, size, and data type. In the HDF SD API, you use the SDgetinfo routine to gather this information. In MATLAB, use the hdfsd function, specifying as arguments:

This code retrieves information about the data set identified by sds_id:

Step 6: Reading Data from the HDF File.   To read data from an HDF4 file, you must use the SDreaddata routine. In MATLAB, use the hdfsd function, specifying as arguments:

For example, to read the entire contents of a data set, use this code:

  1. To read less than the entire data set, use the start, stride, and edges vectors to specify where you want to start reading data and how much data you want to read. For example, this code reads the entire second row of the sample data set:

Step 7: Closing the HDF4 Data Set.   After writing data to a data set in an HDF file, you must close access to the data set. In the HDF SD API, you use the SDendaccess routine to close a data set. In MATLAB, use the hdfsd function, specifying as arguments:

For example, this code closes the data set:

You must close access to all the data sets in an HDF file before closing it.

Step 8: Closing the HDF File.   After writing data to a data set and closing the data set, you must also close the HDF file. In the HDF SD API, you use the SDend routine. In MATLAB, use the hdfsd function, specifying as arguments:

For example, this code closes the data set:


Previous page  Using the MATLAB hdfread Function Exporting MATLAB Data to an HDF4 File Next page

© 1994-2005 The MathWorks, Inc.