Development Environment    

The HDF SD Export Programming Model

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

  1. Create the HDF file, or open an existing one.
  2. Create a data set in the file, or select an existing one.
  3. Write data to the data set.
  4. Close access to the data set and the HDF file.

You can optionally include information in the HDF file that describes your data. See Including Metadata in an HDF File for more information.

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 Opening HDF Files.

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 create as the first argument. To write data to an existing data set, you must obtain the HDF SD data set identifier. See Selecting Data Sets in HDF Files for more information.

This table lists the other arguments to SDcreate.

Argument
MATLAB Data Type
Valid HDF SD file identifier
Returned from SDstart
Name you want assigned to the data set
Text string
Data type of the data set
Text string. For information about specifying data types, see Importing HDF Data.
Number of dimensions in the data set. This is called the rank of the data set in HDF terminology.
Scalar numeric value
Size of each dimension
Vector

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 Writing MATLAB Data to an HDF File.

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 writedata as the first argument.

This table lists the other arguments to SDwritedata.

Argument
MATLAB Data Type
Valid data set identifier (sds_id)
Returned by SDcreate
Location in the data set where you want to start writing data, called the start vector in HDF terminology
Vector of index values
Number of elements along each dimension to skip between each write operation, called the stride vector in HDF terminology
Vector of scalar values
Total number of elements to write along each dimension, called the edges vector in HDF terminology
Vector of scalar values
MATLAB array to be written
Array of doubles

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.

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:

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.


  Exporting MATLAB Data in an HDF File Including Metadata in an HDF File