Programming |
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:
create
in this case
sd_id
, returned by SDstart
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:
ds_name = 'A'; ds_type = 'double'; ds_rank = ndims(A); ds_dims = fliplr(size(A)); sds_id = hdfsd('create',sd_id,ds_name,ds_type,ds_rank,ds_dims);
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:
writedata
in this case
sds_id
, returned by SDcreate
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,
ds_start = zeros(1:ndims(A)); % Start at the beginning ds_stride = []; % Write every element. ds_edges = fliplr(size(A)); % Reverse the dimensions. stat = hdfsd('writedata',sds_id,... ds_start, ds_stride, ds_edges, A)
If it can write the data to the data set, SDwritedata
returns 0
; otherwise, it returns -1
.
Note
SDwritedata queues write operations. To ensure that these queued write operations are executed, you must close the file, using the SDend routine. See Step 6: Closing an HDF File for more information. As a convenience, MATLAB provides a function, MLcloseall , that you can use to close all open data sets and file identifiers with a single call. See Using the MATLAB HDF Utility API for more information.
|
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:
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.
ds_start = [0 1]; % Start writing at the first column, second row. ds_stride = []; % Write every element. ds_edges = [5 1]; % Each row is a 1-by-5 vector. stat = hdfsd('writedata',sds_id,ds_start,ds_stride,ds_edges,B);
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
sd_id
), a data set identifier (sds_id
), or a dimension identifier (dim_id
).
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.
Note
Closing an HDF file executes all the write operations that have been queued using SDwritedata . As a convenience, the MATLAB HDF Utility API provides a function, MLcloseall , that can close all open data set and file identifiers with a single call. See Using the MATLAB HDF Utility API for more information.
|
Exporting MATLAB Data to an HDF4 File | Using the MATLAB HDF Utility API |
© 1994-2005 The MathWorks, Inc.