| MATLAB® | ![]() |
| On this page… |
|---|
Using the HDF Import Tool Subsetting Options |
Note For information about importing HDF5 data, which is a separate, incompatible format, see Hierarchical Data Format (HDF5) Files. |
Hierarchical Data Format (HDF4) is a general-purpose, machine-independent standard for storing scientific data in files, developed by the National Center for Supercomputing Applications (NCSA). For more information about these file formats, read the HDF documentation at the HDF Web site (www.hdfgroup.org).
HDF-EOS is an extension of HDF4 that was developed by the National Aeronautics and Space Administration (NASA) for storage of data returned from the Earth Observing System (EOS). For more information about this extension to HDF4, see the HDF-EOS documentation at the NASA Web site (www.hdfeos.org).
The HDF Import Tool is a graphical user interface that you can use to navigate through HDF4 or HDF-EOS files and import data from them. Importing data using the HDF Import Tool involves these steps:
The following sections provide more detail about each of these steps.
Open an HDF4 or HDF-EOS file in MATLAB® using one of the following methods:
Choose the Import Data option from the MATLAB File menu. If you select an HDF4 or HDF-EOS file, the MATLAB Import Wizard automatically starts the HDF Import Tool.
Start the HDF Import Tool by entering the hdftool command at the MATLAB command line:
hdftool
This opens an empty HDF Import Tool. To open a file, click the Open option on the HDFTool File menu and select the file you want to open. You can open multiple files in the HDF Import Tool.
Open an HDF or HDF-EOS file by specifying the file name with the hdftool command on the MATLAB command line:
hdftool('example.hdf')
Viewing a File in the HDF Import Tool. When you open an HDF4 or HDF-EOS file in the HDF Import Tool, the tool displays the contents of the file in the Contents pane. You can use this pane to navigate within the file to see what data sets it contains. You can view the contents of HDF-EOS files as HDF data sets or as HDF-EOS files. The icon in the contents pane indicates the view, as illustrated in the following figure. Note that these are just two views of the same data.

To import a data set, you must first select the data set in the contents pane of the HDF Import Tool. Use the Contents pane to view the contents of the file and navigate to the data set you want to import.
For example, the following figure shows the data set Example SDS in the HDF file selected. Once you select a data set, the Metadata panel displays information about the data set and the importing and subsetting pane displays subsetting options available for this type of HDF object.

When you select a data set in the contents pane, the importing and subsetting pane displays the subsetting options available for that type of HDF object. The subsetting options displayed vary depending on the type of HDF object. For more information, see Using the HDF Import Tool Subsetting Options.
To import the data set you have selected, click the Import button, bottom right corner of the Importing and Subsetting pane. Using the Importing and Subsetting pane, you can
Specify the name of the workspace variable — By default, the HDF Import Tool uses the name of the HDF4 data set as the name of the MATLAB workspace variable. In the following figure, the variable name is Example_SDS. To specify another name, enter text in the Workspace Variable text box.
Specify whether to import metadata associated with the data set — To import any metadata that might be associated with the data set, select the Import Metadata check box. To store the metadata, the HDF Import Tool creates a second variable in the workspace with the same name with "_info" appended to it. For example, if you select this check box, the name of the metadata variable for the data set in the figure would be Example_SDS_info.
Save the data set import command syntax — The Dataset import command text window displays the MATLAB command used to import the data set. This text is not editable, but you can copy and paste it into the MATLAB Command Window or a text editor for reuse.
The following figure shows how to specify these options in the HDF Import Tool.

To close a file, select the file in the contents pane and click Close File on the HDF Import Tool File menu.
To close all the files open in the HDF Import Tool, click Close All Files on the HDF Import Tool File menu.
To close the tool, click Close HDFTool in the HDF Import Tool File menu or click the Close button in the upper right corner of the tool.
If you used the hdftool syntax that returns a handle to the tool,
h = hdftool('example.hdf')you can use the close(h) command to close the tool from the MATLAB command line.
When you select a data set, the importing and subsetting pane displays the subsetting options available for that type of data set. The following sections provide information about these subsetting options for all supported data set types. For general information about the HDF Import tool, see Using the HDF Import Tool.
Note To use these data subsetting options effectively, you must understand the HDF and HDF-EOS data formats. Therefore, use this documentation in conjunction with the HDF documentation (www.hdfgroup.org) and the HDF-EOS documentation (www.hdfeos.org). |
The HDF scientific data set (SD) is a group of data structures used to store and describe multidimensional arrays of scientific data. Using the HDF Import Tool subsetting parameters, you can import a subset of an HDF scientific data set by specifying the location, range, and number of values to be read along each dimension.

The subsetting parameters are:
Start — Specifies the position on the dimension to begin reading. The default value is 1, which starts reading at the first element of each dimension. The values specified must not exceed the size of the relevant dimension of the data set.
Increment — Specifies the interval between the values to read. The default value is 1, which reads every element of the data set.
Length — Specifies how much data to read along each dimension. The default value is the length of the dimension, which causes all the data to be read.
HDF Vdata data sets provide a framework for storing customized tables. A Vdata table consists of a collection of records whose values are stored in fixed-length fields. All records have the same structure and all values in each field have the same data type. Each field is identified by a name. The following figure illustrates a Vdata table.

You can import a subset of an HDF Vdata data set in the following ways:
Specifying the name of the field that you want to import
Specifying the range of records that you want to import
The following figure shows how you specify these subsetting parameters for Vdata.

In HDF-EOS Grid data, a rectilinear grid overlays a map. The map uses a known map projection. The HDF Import Tool supports the following mutually exclusive subsetting options for Grid data:
To access these options, click the Subsetting method menu in the importing and subsetting pane.

Direct Index. You can import a subset of an HDF-EOS Grid data set by specifying the location, range, and number of values to be read along each dimension.

Each row represents a dimension in the data set and each column represents these subsetting parameters:
Start — Specifies the position on the dimension to begin reading. The default value is 1, which starts reading at the first element of each dimension. The values specified must not exceed the size of the relevant dimension of the data set.
Increment — Specifies the interval between the values to read. The default value is 1, which reads every element of the data set.
Length — Specifies how much data to read along each dimension. The default value is the length of the dimension, which causes all the data to be read.
Geographic Box. You can import a subset of an HDF-EOS Grid data set by specifying the rectangular area of the grid that you are interested in. To define this rectangular area, you must specify two points, using longitude and latitude in decimal degrees. These points are two corners of the rectangular area. Typically, Corner 1 is the upper-left corner of the box, and Corner 2 is the lower-right corner of the box.

Optionally, you can further define the subset of data you are interested in by using Time parameters (see Time) or by specifying other User-Defined subsetting parameters (see User-Defined).
Interpolation. Interpolation is the process of estimating a pixel value at a location in between other pixels. In interpolation, the value of a particular pixel is determined by computing the weighted average of some set of pixels in the vicinity of the pixel.
You define the region used for bilinear interpolation by specifying two points that are corners of the interpolation area:
Corner 1 – Specify longitude and latitude values in decimal degrees. Typically, Corner 1 is the upper-left corner of the box.
Corner 2 — Specify longitude and latitude values in decimal degrees. Typically, Corner 2 is the lower-right corner of the box

Pixels. You can import a subset of the pixels in a Grid data set by defining a rectangular area over the grid. You define the region used for bilinear interpolation by specifying two points that are corners of the interpolation area:
Corner 1 – Specify longitude and latitude values in decimal degrees. Typically, Corner 1 is the upper-left corner of the box.
Corner 2 — Specify longitude and latitude values in decimal degrees. Typically, Corner 2 is the lower-right corner of the box
![]()
Tile. In HDF-EOS Grid data, a rectilinear grid overlays a map. Each rectangle defined by the horizontal and vertical lines of the grid is referred to as a tile. If the HDF-EOS Grid data is stored as tiles, you can import a subset of the data by specifying the coordinates of the tile you are interested in. Tile coordinates are 1-based, with the upper-left corner of a two-dimensional data set identified as 1,1. In a three-dimensional data set, this tile would be referenced as 1,1,1.

Time. You can import a subset of the Grid data set by specifying a time period. You must specify both the start time and the stop time (the endpoint of the time span). The units (hours, minutes, seconds) used to specify the time are defined by the data set.

Along with these time parameters, you can optionally further define the subset of data to import by supplying user-defined parameters.
User-Defined. You can import a subset of the Grid data set by specifying user-defined subsetting parameters.

When specifying user-defined parameters, you must first specify whether you are subsetting along a dimension or by field. Select the dimension or field by name using the Dimension or Field Name menu. Dimension names are prefixed with the characters DIM:.
Once you specify the dimension or field, you use Min and Max to specify the range of values that you want to import. For dimensions, Min and Max represent a range of elements. For fields, Min and Max represent a range of values.
HDF-EOS Point data sets are tables. You can import a subset of an HDF-EOS Point data set by specifying field names and level. Optionally, you can refine the subsetting by specifying the range of records you want to import, by defining a rectangular area, or by specifying a time period. For information about specifying a rectangular area, see Geographic Box. For information about subsetting by time, see Time.

HDF-EOS Swath data is data that is produced by a satellite as it traces a path over the earth. This path is called its ground track. The sensor aboard the satellite takes a series of scans perpendicular to the ground track. Swath data can also include a vertical measure as a third dimension. For example, this vertical dimension can represent the height above the Earth of the sensor.
The HDF Import Tool supports the following mutually exclusive subsetting options for Swath data:
To access these options, click the Subsetting method menu in the Importing and Subsetting pane.

Direct Index. You can import a subset of an HDF-EOS Swath data set by specifying the location, range, and number of values to be read along each dimension.

Each row represents a dimension in the data set and each column represents these subsetting parameters:
Start — Specifies the position on the dimension to begin reading. The default value is 1, which starts reading at the first element of each dimension. The values specified must not exceed the size of the relevant dimension of the data set.
Increment — Specifies the interval between the values to read. The default value is 1, which reads every element of the data set.
Length — Specifies how much data to read along each dimension. The default value is the length of the dimension, which causes all the data to be read.
Geographic Box. You can import a subset of an HDF-EOS Swath data set by specifying the rectangular area of the grid that you are interested in and by specifying the selection Mode.

You define the rectangular area by specifying two points that specify two corners of the box:
Corner 1 — Specify longitude and latitude values in decimal degrees. Typically, Corner 1 is the upper-left corner of the box.
Corner 2 — Specify longitude and latitude values in decimal degrees. Typically, Corner 2 is the lower-right corner of the box.
You specify the selection mode by choosing the type of Cross Track Inclusion and the Geolocation mode. The Cross Track Inclusion value determines how much of the area of the geographic box that you define must fall within the boundaries of the swath.

Select from these values:
AnyPoint — Any part of the box overlaps with the swath.
Midpoint — At least half of the box overlaps with the swath.
Endpoint — All of the area defined by the box overlaps with the swath.
The Geolocation Mode value specifies whether geolocation fields and data must be in the same swath.

Select from these values:
Internal — Geolocation fields and data fields must be in the same swath.
External — Geolocation fields and data fields can be in different swaths.
Time. You can optionally also subset swath data by specifying a time period. The units used (hours, minutes, seconds) to specify the time are defined by the data set

User-Defined. You can optionally also subset a swath data set by specifying user-defined parameters.

When specifying user-defined parameters, you must first specify whether you are subsetting along a dimension or by field. Select the dimension or field by name using the Dimension or Field Name menu. Dimension names are prefixed with the characters DIM:.
Once you specify the dimension or field, you use Min and Max to specify the range of values that you want to import. For dimensions, Min and Max represent a range of elements. For fields, Min and Max represent a range of values.
For 8-bit HDF raster image data, you can specify the colormap.
To import data from an HDF or HDF-EOS file, you can use the MATLAB HDF4 high-level function hdfread. The hdfread function provides a programmatic way to import data from an HDF4 or HDF-EOS file that still hides many of the details that you need to know if you use the low-level HDF functions, described in Using the HDF4 Low-Level Functions. You can also import HDF4 data using an interactive GUI, described in Using the HDF Import Tool.
This section describes these high-level MATLAB HDF functions, including
To export data to an HDF4 file, you must use the MATLAB HDF4 low-level functions.
To get information about the contents of an HDF4 file, use the hdfinfo function. The hdfinfo function returns a structure that contains information about the file and the data in the file.
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. |
This example returns information about a sample HDF4 file included with MATLAB:
info = hdfinfo('example.hdf')
info =
Filename: 'example.hdf'
SDS: [1x1 struct]
Vdata: [1x1 struct]To get information about the data sets stored in the file, look at the SDS field.
To use thehdfread function, you must specify the data set that you want to read. You can specify the filename and the data set name as arguments, or you can specify a structure returned by the hdfinfo function that contains this information. The following example shows both methods. For information about how to import a subset of the data in a data set, see Reading a Subset of the Data in a Data Set.
Determine the names of data sets in the HDF4 file, using the hdfinfo function.
info = hdfinfo('example.hdf')
info =
Filename: 'example.hdf'
SDS: [1x1 struct]
Vdata: [1x1 struct]To determine the names and other information about the data sets in the file, look at the contents of the SDS field. The Name field in the SDS structure gives the name of the data set.
dsets = info.SDS
dsets =
Filename: 'example.hdf'
Type: 'Scientific Data Set'
Name: 'Example SDS'
Rank: 2
DataType: 'int16'
Attributes: []
Dims: [2x1 struct]
Label: {}
Description: {}
Index: 0Read the data set from the HDF4 file, using the hdfread function. Specify the name of the data set as a parameter to the function. Note that the data set name is case sensitive. This example returns a 16-by-5 array:
dset = hdfread('example.hdf', 'Example SDS');
dset =
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
6 7 8 9 10
7 8 9 10 11
8 9 10 11 12
9 10 11 12 13
10 11 12 13 14
11 12 13 14 15
12 13 14 15 16
13 14 15 16 17
14 15 16 17 18
15 16 17 18 19
16 17 18 19 20
17 18 19 20 21
18 19 20 21 22Alternatively, you can specify the specific field in the structure returned by hdfinfo that contains this information. For example, to read a scientific data set, use the SDS field.
dset = hdfread(info.SDS);
Reading a Subset of the Data in a Data Set. To read a subset of a data set, you can use the optional 'index' parameter. The value of the index parameter is a cell array of three vectors that specify the location in the data set to start reading, the skip interval (e.g., read every other data item), and the amount of data to read (e.g., the length along each dimension). In HDF4 terminology, these parameters are called the start, stride, and edge values.
For example, this code
Starts reading data at the third row, third column ([3 3]).
Reads every element in the array ([]).
Reads 10 rows and 2 columns ([10 2]).
subset = hdfread('Example.hdf','Example SDS',...
'Index',{[3 3],[],[10 2 ]})
subset =
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17This 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 or export data from the MATLAB workspace into an HDF4 file. To import or export data, you must use the functions in the HDF4 API associated with the particular HDF4 data type you are working with. Each API has a particular programming model, that is, a prescribed way to use the routines to write data sets to the file. To illustrate this concept, this section describes the programming model of one particular HDF4 API: the HDF4 Scientific Data (SD) API. For a complete list of the HDF4 APIs supported by MATLAB and the functions you use to access each one, see the hdf reference page.
Note This section does not attempt to describe all HDF4 features and routines. To use the MATLAB HDF4 functions effectively, you must refer to the official NCSA documentation at the HDF Web site (www.hdfgroup.org). |
Topics covered include
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 HDF4 Scientific Data (SD) API includes separate C routines to open (SDopen), close (SDend), and read data (SDreaddata).
Instead of supporting each routine in the HDF4 APIs, MATLAB provides a single function that serves as a gateway to all the routines in a particular HDF4 API. For example, the HDF Scientific Data (SD) API includes the C routine SDend to close an HDF4 file:
status = SDend(sd_id); /* C code */
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,
status = hdfsd('end',sd_id); % MATLAB codeHandling HDF4 Routines with Output Arguments. Some HDF4 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 HDF4 file in two output arguments, ndatasets and nglobal_atts. Here is the C code:
status = SDfileinfo(sd_id, ndatasets, nglobal_atts);
To call this routine from MATLAB, change the output arguments into return values:
[ndatasets, nglobal_atts, status] = hdfsd('fileinfo',sd_id);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.
To illustrate using HDF4 API routines in MATLAB, the following sections provide a step-by-step example of how to import HDF4 Scientific Data (SD) into the MATLAB workspace.
Note The following sections, when referring to specific routines in the HDF4 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 HDF4 SD data set, you must first open the file using the SD API routine SDstart. (In HDF4 terminology, the numeric arrays stored in HDF4 files are called data sets.) In MATLAB, you use the hdfsd function, specifying as arguments:
Name of the SD API routine, start in this case.
Name of the file you want to open.
Mode in which you want to open it. The following table lists the file access modes supported by the SDstart routine. In MATLAB, you specify these modes as text strings. You can specify the full HDF4 mode name or one of the abbreviated forms listed in the table.
HDF4 File Creation Mode | HDF4 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:
sd_id = hdfsd('start','mydata.hdf','read');If SDstart can find and open the file specified, it returns an HDF4 SD file identifier, named sd_id in the example. Otherwise, it returns -1.
Step 2: Retrieving Information About the HDF4 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 Example: Exporting Data Using the HDF4 SD API Functions.) In MATLAB, you use the hdfsd function, specifying the following arguments:
Name of the SD API routine, fileinfo in this case
SD file identifier, sd_id, returned by SDstart
In this example, the HDF4 file contains three data sets and one global attribute.
[ndatasets, nglobal_atts, stat] = hdfsd('fileinfo',sd_id)
ndatasets =
3
nglobal_atts =
1
status =
0Step 3: Retrieving Attributes from an HDF4 File (Optional). HDF4 files can optionally include information, called attributes, that describes the data the file contains. Attributes associated with an entire HDF4 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 HDF4 File.)
To retrieve attributes from an HDF4 file, use the HDF4 API routine SDreadattr. In MATLAB, use the hdfsd function, specifying as arguments:
Name of the SD API routine, readattr in this case.
File identifier (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.)
Index identifying the attribute you want to view. HDF4 uses zero-based indexing. If you know the name of an attribute but not its index, use the 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:
attr_idx = 0;
[attr, status] = hdfsd('readattr', sd_id, attr_idx);
attr =
my global attributeStep 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:
Name of the SD API routine, select in this case
HDF4 SD file identifier (sd_id) returned by SDstart
If SDselect finds the specified data set in the file, it returns an HDF4 SD data set identifier, called sds_id in the example. If it cannot find the data set, it returns -1.
Note Do not confuse HDF4 SD file identifiers, named sd_id in the examples, with HDF4 SD data set identifiers, named sds_id in the examples. |
sds_id = hdfsd('select',sd_id,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 HDF4 SD API, you use the SDgetinfo routine to gather this information. In MATLAB, use the hdfsd function, specifying as arguments:
Name of the SD API routine, getinfo in this case
HDF4 SD data set identifier (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 =
0Step 6: Reading Data from the HDF4 File. To read data from an HDF4 file, you must use the SDreaddata routine. In MATLAB, use the hdfsd function, specifying as arguments:
Name of the SD API function, readdata in this case.
HDF4 SD data set identifier (sds_id) returned by SDselect.
Location in the data set where you want to start reading data, specified as a vector of index values, called the start vector. To read from the beginning of a data set, specify zero for each element of the start vector. Use SDgetinfo to determine the dimensions of the data set.
Number of elements along each dimension to skip between each read operation, specified as a vector of scalar values, called the stride vector. To read every element of a data set, specify 1 as the value for each element of the vector or specify an empty array ([]).
Total number of elements to read along each dimension, specified as a vector of scalar values, called the edges vector. To read every element of a data set, set each element of the edges vector to the size of each dimension of the data set. Use SDgetinfo to determine these sizes.
Note SDgetinfo returns dimension values in row-major order, the ordering used by HDF4. Because MATLAB stores data in column-major order, you must specify the dimensions in column-major order, that is, [columns,rows]. In addition, you must use zero-based indexing in these arguments. |
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 15To 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:
ds_start = [0 1]; % Start reading at the first column, second row
ds_stride = []; % Read each element
ds_edges = [5 1]; % Read a 1-by-5 vector of data
[ds_data, status] =
hdfsd('readdata',sds_id,ds_start,ds_stride,ds_edges);Step 7: Closing the HDF4 Data Set. After writing data to a data set in an HDF4 file, you must close access to the data set. In the HDF4 SD API, you use the SDendaccess routine to close a data set. In MATLAB, use the hdfsd function, specifying as arguments:
Name of the SD API routine, endaccess in this case
HDF4 SD data set identifier (sds_id) returned by SDselect
For example, this code closes the data set:
stat = hdfsd('endaccess',sds_id);You must close access to all the data sets in an HDF4 file before closing it.
Step 8: Closing the HDF4 File. After writing data to a data set and closing the data set, you must also close the HDF4 file. In the HDF4 SD API, you use the SDend routine. In MATLAB, use the hdfsd function, specifying as arguments:
Name of the SD API routine, end in this case
HDF4 SD file identifier (sd_id) returned by SDstart
For example, this code closes the data set:
stat = hdfsd('end',sd_id);The following sections provide a step-by-step example of how to export data from the MATLAB workspace to an HDF4 file using Scientific Data (SD) API functions.
Step 1: Creating an HDF4 File. To export MATLAB data in HDF4 format, you must first create an HDF4 file, or open an existing one. In the HDF4 SD API, you use the SDstart routine. In MATLAB, use the hdfsd function, specifying start as the first argument. As other arguments, specify
A text string specifying the name you want to assign to the HDF4 file (or the name of an existing HDF4 file)
A text string specifying the HDF4 SD interface file access mode
For example, this code creates an HDF4 file named mydata.hdf:
sd_id = hdfsd('start','mydata.hdf','DFACC_CREATE');When you specify the DFACC_CREATE access mode, SDstart creates the file and initializes the HDF4 SD multifile interface, returning an HDF4 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 HDF4 file, you must use HDF4 read or write modes. For information about using SDstart in these modes, see Step 1: Opening the HDF4 File.
Step 2: Creating an HDF4 Data Set. After creating the HDF4 file, or opening an existing one, you must create a data set in the file for each MATLAB array you want to export. If you are writing data to an existing data set, you can skip ahead to the next step.
In the HDF4 SD API, you use the SDcreate routine to create data sets. In MATLAB, you use the hdfsd function, specifying as arguments:
Name of the SD API routine, 'create' in this case
Valid HDF4 SD file identifier, sd_id, returned by SDstart
Name you want assigned to the data set
Data type of the data set.
Number of dimensions in the data set. This is called the rank of the data set in HDF4 terminology.
Size of each dimension, specified as a vector
Once you create a data set, you cannot change its name, data type, or dimensions.
For example, to create a data set in which you can write the following MATLAB 3-by-5 array of doubles,
A = [ 1 2 3 4 5 ; 6 7 8 9 10 ; 11 12 13 14 15 ];
you could call hdfsd, specifying as arguments 'create' and a valid HDF file identifier, sd_id. In addition, set the values of the other 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 HDF4 SD data set identifier, (sds_id). Otherwise, SDcreate returns -1.
In this example, note the following:
The data type you specify in ds_type must match the data type of the MATLAB array that you want to write to the data set. In the example, the array is of class double so the value of ds_type is set to 'double'. If you wanted to use another data type, such as uint8, convert the MATLAB array to use this data type,
A = uint8([ 1 2 3 4 5 ; 6 7 8 9 10 ; 11 12 13 14 15 ]);
and specify the name of the MATLAB data type, uint8 in this case, in the ds_type argument.
ds_type = 'uint8';
The code fragment reverses the order of the values in the dimensions argument (ds_dims). This processing is necessary because the MATLAB size function returns the dimensions in column-major order and HDF4 expects to receive dimensions in row-major order.
Step 3: Writing MATLAB® Data to an HDF4 File. After creating an HDF4 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 HDF4 SD API, you use the SDwritedata routine. In MATLAB, use the hdfsd function, specifying as arguments:
Name of the SD API routine, 'writedata' in this case
Valid HDF4 SD 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 HDF4 terminology
Number of elements along each dimension to skip between each write operation, called the stride vector in HDF4 terminology
Total number of elements to write along each dimension, called the edges vector in HDF4 terminology
MATLAB array to be written
Note You must specify the values of the start, stride, and edges arguments in row-major order, rather than the column-major order used in MATLAB. Note how the example uses fliplr to reverse the order of the dimensions in the vector returned by the size function before assigning it as the value of the edges argument. |
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,
A = [ 1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15 ];
into an HDF4 file:
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 HDF4 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® HDF4 Utility API for more information. |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
with the vector B:
B = [ 9 9 9 9 9];
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 HDF4 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 HDF4 File. You can optionally include information in an HDF4 file, called attributes, that describes the file and its contents. Using the HDF4 SD API, you can associate attributes with three types of HDF4 objects:
An entire HDF4 file — File attributes, also called global attributes, generally contain information pertinent to all the data sets in the file.
A data set in an HDF4 file — Data set attributes, also called local attributes, describe individual data sets.
A dimension of a data set — Dimension attributes provide information about one particular dimension of a data set.
To create an attribute in the HDF4 SD API, use the SDsetattr routine. In MATLAB, use the hdfsd function, specifying 'setattr' as the first argument. As other arguments, specify
A valid HDF4 SD identifier associated with the object. This value can be a file identifier (sd_id), a data set identifier (sds_id), or a dimension identifier (dim_id).
A text string that defines the name of the attribute.
The attribute value.
For example, this code creates a global attribute, named my_global_attr, and associates it with the HDF4 file identified by sd_id:
status = hdfsd('setattr',sd_id,'my_global_attr','my_attr_val');Note In the NCSA documentation, the SDsetattr routine has two additional arguments: data type and the number of values in the attribute. When calling this routine from MATLAB, you do not have to include these arguments. The MATLAB HDF4 function can determine the data type and size of the attribute from the value you specify. |
The SD interface supports predefined attributes that have reserved names and, in some cases, data types. Predefined attributes are identical to user-defined attributes except that the HDF4 SD API has already defined their names and data types. For example, the HDF4 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 HDF4 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 HDF4 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:
attr_name = 'cordsys';
attr_value = 'polar';
status = hdfsd('setattr',sds_id,attr_name,attr_value);The HDF4 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.
You can associate multiple attributes with a single HDF4 object. HDF4 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 HDF4 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.
Step 5: Closing HDF4 Data Sets. After writing data to a data set in an HDF4 file, you must close access to the data set. In the HDF4 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 HDF4 SD data set identifier, sds_id in this example:
stat = hdfsd('endaccess',sds_id);Step 6: Closing an HDF4 File. After writing data to a data set and closing the data set, you must also close the HDF4 file. In the HDF4 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 HDF4 SD file identifier, sd_id in this example:
stat = hdfsd('end',sd_id);You must close access to all the data sets in an HDF4 file before closing it.
Note Closing an HDF4 file executes all the write operations that have been queued using SDwritedata. As a convenience, the MATLAB HDF Utility API provides a function that can close all open data set and file identifiers with a single call. See Using the MATLAB® HDF4 Utility API for more information. |
In addition to the standard HDF4 APIs, listed in the hdfreference page, MATLAB supports utility functions that are designed to make it easier to use HDF4 in the MATLAB environment.
For example, using the gateway function to the MATLAB HDF4 utility API, hdfml, and specifying the name of the listinfo routine as an argument, you can view all the currently open HDF4 identifiers. MATLAB updates this list whenever HDF identifiers are created or closed. In the following example only two identifiers are open.
hdfml('listinfo')
No open RI identifiers
No open GR identifiers
No open grid identifiers
No open grid file identifiers
No open annotation identifiers
No open AN identifiers
Open scientific dataset identifiers:
262144
Open scientific data file identifiers:
393216
No open Vdata identifiers
No open Vgroup identifiers
No open Vfile identifiers
No open point identifiers
No open point file identifiers
No open swath identifiers
No open swath file identifiers
No open access identifiers
No open file identifiersClosing All Open HDF4 Identifiers. To close all the currently open HDF4 identifiers in a single call, use the gateway function to the MATLAB HDF4 utility API, hdfml, specifying the name of the closeall routine as an argument. The following example closes all the currently open HDF4 identifiers.
hdfml('closeall')
![]() | Hierarchical Data Format (HDF5) Files | Error Handling | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |