Main Content

Import HDF4 Files Using High-Level Functions


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 (

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 (

MATLAB® includes several options for importing HDF4 files, discussed in the following sections.


For information about importing HDF5 data, which is a separate, incompatible format, see Import HDF5 Files.

Using the MATLAB HDF4 High-Level Functions

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 Import HDF4 Files Using Low-Level Functions.

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.

Using hdfinfo to Get Information About an HDF4 File

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.

This example returns information about a sample HDF4 file included with MATLAB:

info = hdfinfo('example.hdf')
info = 

      Filename: 'matlabroot\example.hdf'
    Attributes: [1x2 struct]
        Vgroup: [1x1 struct]
           SDS: [1x1 struct]
         Vdata: [1x1 struct]

To get information about the data sets stored in the file, look at the SDS field.

Using hdfread to Import Data from an HDF4 File

To use the hdfread 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.

  1. Determine the names of data sets in the HDF4 file, using the hdfinfo function.

    info = hdfinfo('example.hdf')
    info = 
          Filename: 'matlabroot\example.hdf'
        Attributes: [1x2 struct]
            Vgroup: [1x1 struct]
               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: 0
  2. Read 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     22

    Alternatively, 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     17