Programming Previous page   Next Page

Example: Exporting Data to an HDF4 File

The programming model for exporting HDF SD data involves these steps:

Step 1: Creating an HDF File

To export MATLAB data in HDF format, you must first create an HDF file, or open an existing one. In the HDF SD API, you use the SDstart routine. In MATLAB, use the hdfsd function, specifying start as the first argument. As other arguments, specify

For example, this code creates an HDF file named mydata.hdf:

When you specify the DFACC_CREATE access mode, SDstart creates the file and initializes the HDF SD multifile interface, returning an HDF SD file identifier, named sd_id in the example.

If you specify DFACC_CREATE mode and the file already exists, SDstart fails, returning -1. To open an existing HDF file, you must use HDF read or write modes. For information about using SDstart in these modes, see Step 1: Opening the HDF4 File.

Step 2: Creating an HDF Data Set

After creating the HDF file, or opening an existing one, you must create a data set in the file for each MATLAB array you want to export.

In the HDF SD API, you use the SDcreate routine to create data sets. In MATLAB, you use the hdfsd function, specifying as arguments:

The values you assign to these arguments depend on the MATLAB array you want to export. For example, to export the following MATLAB 3-by-5 array of doubles,

you could set the values of these arguments as in this code fragment:

If SDcreate can successfully create the data set, it returns an HDF SD data set identifier, (sds_id). Otherwise, SDcreate returns -1.

Once you create a data set, you cannot change its characteristics. You can, however, modify the data it contains. To do this, initiate access to the data set, using SDselect, and write to the data set as described in Step 3: Writing MATLAB Data to an HDF File.

Step 3: Writing MATLAB Data to an HDF File

After creating an HDF file and creating a data set in the file, you can write data to the entire data set or just a portion of the data set. In the HDF SD API, you use the SDwritedata routine. In MATLAB, use the hdfsd function, specifying specifying as arguments:

The values you assign to these arguments depend on the MATLAB array you want to export. For example, the following code fragment writes this MATLAB 3-by-5 array of doubles,

into an HDF file:

If it can write the data to the data set, SDwritedata returns 0; otherwise, it returns -1.

Writing Data to Portions of Data Sets.   To write less than the entire data set, use the start, stride, and edges vectors to specify where you want to start writing data and how much data you want to write.

For example, the following code fragment uses SDwritedata to replace the values of the entire second row of the sample data set:

with the vector B:

In the example, the start vector specifies that you want to start the write operation in the first column of the second row. Note how HDF uses zero-based indexing and specifies the column dimension first. In MATLAB, you would specify this location as (2,1). The edges argument specifies the dimensions of the data to be written. Note that the size of the array of data to be written must match the edge specification.

Step 4: Writing Metadata to an HDF File

You can optionally include information in an HDF file, called attributes, that describes the file and its contents. Using the HDF SD API, you can associate attributes with three types of HDF objects:

Associating Multiple Attributes with a Single Object.   You can associate multiple attributes with a single HDF object. HDF maintains an attribute index for each object. The attribute index is zero-based. The first attribute has index value 0, the second has index value 1, and so on. You access an attribute by its index value.

Each attribute has the format name=value, where name (called label in HDF terminology) is a text string up to 256 characters in length and value contains one or more entries of the same data type. A single attribute can have multiple values.

Creating Attributes.   To create an attribute in the HDF SD API, use the SDsetattr routine. In MATLAB, use the hdfsd function, specifying setattr as the first argument. As other arguments, specify

For example, this code creates a global attribute, named my_global_attr, and associates it with the HDF file identified by sd_id:

Creating Predefined Attributes.   Predefined attributes are identical to user-defined attributes except that the HDF SD API has already defined their names and data types. For example, the HDF SD API defines an attribute, named cordsys, in which you can specify the coordinate system used by the data set. Possible values of this attribute include the text strings 'cartesian', 'polar', and 'spherical'.

Predefined attributes can be useful because they establish conventions that applications can depend on. The HDF SD API supports predefined attributes for data sets and dimensions only; there are no predefined attributes for files. For a complete list of the predefined attributes, see the NCSA documentation.

In the HDF SD API, you create predefined attributes the same way you create user-defined attributes, using the SDsetattr routine. In MATLAB, use the hdfsd function, specifying setattr as the first argument:

The HDF SD API also includes specialized functions for writing and reading the predefined attributes. These specialized functions, such as SDsetdatastrs, are sometimes easier to use, especially when you are reading or writing multiple related predefined attributes. You must use specialized functions to read or write the predefined dimension attributes.

Step 5: Closing HDF Data Sets

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 endaccess as the first argument. As the only other argument, specify a valid HDF SD data set identifier, sds_id in this example:

Step 6: Closing an 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 end as the first argument. As the only other argument, specify a valid HDF SD file identifier, sd_id in this example:

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


Previous page  Exporting MATLAB Data to an HDF4 File Using the MATLAB HDF Utility API Next page

© 1994-2005 The MathWorks, Inc.