Programming |
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.
Note You can also use the HDF Import Tool to get information about the contents of an HDF4 file. See Using the HDF Import Tool for more information. |
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.
Note The following sections, when referring to specific routines in the HDF SD API, use the C library name rather than the MATLAB function name. The MATLAB syntax is used in all examples. |
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:
start
in this case.
SDstart
routine. In MATLAB, you specify these modes as text strings. You can specify the full HDF mode name or one of the abbreviated forms listed in the table.HDF File Creation Mode |
HDF Mode Name |
MATLAB String |
Create a new file |
'DFACC_CREATE' |
'create' |
Read access |
'DFACC_RDONLY' |
'read' or'rdonly' |
Read and write access |
'DFACC_RDWR' |
'rdwr' or'write' |
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:
readattr
in this case.
sd_id
) returned by SDstart
, for global attributes, or the data set identifier for local attributes. (See Step 4: Selecting the Data Sets to Import to learn how to get a data set identifier.)
SDfindattr
routine to determine the index value associated with the attribute.
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
.
Note
Do not confuse HDF SD file identifiers, named sd_id in the examples, with HDF SD data set identifiers, named sds_id in the examples.
|
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:
getinfo
in this case
sds_id
) returned by SDselect
This code retrieves information about the data set identified by sds_id
:
[dsname, dsndims, dsdims, dstype, dsatts, stat] = hdfsd('getinfo',sds_id) dsname = A dsndims = 2 dsdims = 5 3 dstype = double dsatts = 0 stat = 0
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:
readdata
in this case.
sds_id
) returned by SDselect
.
SDgetinfo
to determine the dimensions of the data set.
1
as the value for each element of the vector or specify an empty array ([]
).
SDgetinfo
to determine these sizes.
For example, to read the entire contents of a data set, use this code:
[ds_name, ds_ndims, ds_dims, ds_type, ds_atts, stat] = hdfsd('getinfo',sds_id); ds_start = zeros(1,ds_ndims); % Creates the vector [0 0] ds_stride = []; ds_edges = ds_dims; [ds_data, status] = hdfsd('readdata',sds_id,ds_start,ds_stride,ds_edges) ; disp(ds_data) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
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:
endaccess
in this case
sds_id
) returned by SDselect
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:
Using the MATLAB hdfread Function | Exporting MATLAB Data to an HDF4 File |
© 1994-2005 The MathWorks, Inc.