hdfread - Read data from HDF4 or HDF-EOS2 file - MATLAB (original) (raw)

Read data from HDF4 or HDF-EOS2 file

Syntax

Description

Note

The hdfread function reads HDF4 data from HDF4 files or HDF-EOS2 files. To read data from HDF5 files, use the h5read function.

[data](#mw%5F80299bc8-8724-4d19-8cee-bbfb2d3cb19f) = hdfread([filename](#mw%5F0e4f7e1d-d7d8-4a3a-91fa-86e5f3083552),[SDDatasetName](#mw%5Fe58cb200-6846-41e9-a289-177beeee3638)) returns all the data in the specified HDF Scientific Data (SD) dataset from the specified file.

example

[data](#mw%5F80299bc8-8724-4d19-8cee-bbfb2d3cb19f) = hdfread([filename](#mw%5F0e4f7e1d-d7d8-4a3a-91fa-86e5f3083552),[VDatasetName](#mw%5Fc8b01f89-a772-481e-925a-e1ac932f77a9)) returns all the data in the specified HDF Vertex (V) dataset.

[data](#mw%5F80299bc8-8724-4d19-8cee-bbfb2d3cb19f) = hdfread([filename](#mw%5F0e4f7e1d-d7d8-4a3a-91fa-86e5f3083552),[gridDatasetName](#mw%5F2503ad05-d3c9-4fd9-8e1a-74c84394e21d),Fields=[fields](#mw%5F8d9a0a36-6087-4e0f-889d-a045a42e22e1)) returns all the data in the specified field of the specified HDF-EOS Grid dataset.

example

[data](#mw%5F80299bc8-8724-4d19-8cee-bbfb2d3cb19f) = hdfread([filename](#mw%5F0e4f7e1d-d7d8-4a3a-91fa-86e5f3083552),[pointDatasetName](#mw%5F4faa488d-8500-4132-b8a5-c62ce4909d14),Fields=[fields](#mw%5F8d9a0a36-6087-4e0f-889d-a045a42e22e1),Level=[level](#mw%5Fe69fb114-2752-44d9-8458-60f76367090d)) returns all the data in the specified fields and at the specified level of the specified HDF-EOS Point dataset.

[data](#mw%5F80299bc8-8724-4d19-8cee-bbfb2d3cb19f) = hdfread([filename](#mw%5F0e4f7e1d-d7d8-4a3a-91fa-86e5f3083552),[swathDatasetName](#mw%5F1b0ce6f1-53e3-472f-863c-6dd710786873),Fields=[fields](#mw%5F8d9a0a36-6087-4e0f-889d-a045a42e22e1)) returns all the data in the specified field of the specified HDF-EOS Swath dataset.

[data](#mw%5F80299bc8-8724-4d19-8cee-bbfb2d3cb19f) = hdfread([S](#mw%5Fecdf8254-a1c2-4302-b978-df572248d390)) returns all the data in the dataset specified by the structure S. Use thehdfinfo function to generate the structure. If the generated structure contains more than one dataset, indicate which dataset you want the hdfread function to read by specifying the field in theS structure that relates to a particular type of dataset and using indexing.

example

[data](#mw%5F80299bc8-8724-4d19-8cee-bbfb2d3cb19f) = hdfread(___,[Name=Value](#namevaluepairarguments)) specifies options using one or more name-value arguments in addition to any of the input argument combinations in previous syntaxes. For example, to read 20 records from an HDF V dataset, set NumRecords to 20. For a quick reference on the types of datasets and the name-value arguments they support, see Valid Argument Combinations.

example

[data](#mw%5F80299bc8-8724-4d19-8cee-bbfb2d3cb19f) = hdfread([filename](#mw%5F0e4f7e1d-d7d8-4a3a-91fa-86e5f3083552),[IDatasetName24Bit](#mw%5F04829a57-cae9-4288-9e8d-bfaf1b9a9890)) returns the image data for the specified 24-bit raster image.

[[data](#mw%5F80299bc8-8724-4d19-8cee-bbfb2d3cb19f),[map](#mw%5F98856b2f-112c-4672-ad5f-7fd8a1794fb2)] = hdfread([filename](#mw%5F0e4f7e1d-d7d8-4a3a-91fa-86e5f3083552),[IDatasetName8Bit](#mw%5Fd310c298-5dd6-464b-8a27-889697fc49fa)) returns the image data and the colormap for the specified 8-bit raster image.

Examples

collapse all

Read a dataset named temperature from a sample HDF4 file.

data = hdfread("sd.hdf","temperature");

Check the size of the dataset. Then display the first five rows and columns of the dataset.

ans = 5×5

 1     2     3     4     5
21    22    23    24    25
41    42    43    44    45
61    62    63    64    65
81    82    83    84    85

Retrieve information about the contents of an HDF4 file by using the hdfinfo function. Then extract the structure containing information about the particular dataset that you want to import. This example uses a structure in the SDS field.

S = hdfinfo("sd.hdf"); sds_info = S.SDS(1);

Import the data using the hdfread function.

Read data from the HDF-EOS grid field TbOceanRain from a sample file.

oceanRain = hdfread("example.hdf","MonthlyRain",Fields="TbOceanRain");

Read data for the northern hemisphere for the same field. Use the Box name-value argument to specify the longitude and latitude coordinates for the region.

oceanRainNorth = hdfread("example.hdf","MonthlyRain", ... Fields="TbOceanRain",Box={[0 360] [0 90]});

Retrieve information about a Scientific Data dataset from a sample file.

S = hdfinfo("example.hdf"); sds_info = S.SDS;

Check the size of the dataset.

Read a subset of the data in the dataset using the Index name-value argument. Specify a starting index of [3 3], a stride of 1 for each dimension (by using [] for the second entry of Index), and a count of 10 rows and 2 columns.

data = hdfread(sds_info,Index={[3 3] [] [10 2]})

data = 10×2 int16 matrix

7    8
8    9
9   10

10 11 11 12 12 13 13 14 14 15 15 16 16 17

Read the Idx, Temp, and Dewpt fields of an HDF V dataset. Use the Vdata field in the structure returned by hdfinfo.

S = hdfinfo("example.hdf"); data = hdfread(S.Vdata(1),Fields=["Idx" "Temp" "Dewpt"])

data=3×1 cell array {[ 1 2 3 4 5 6 7 8 9 10]} {[0 12 3 5 10 -1 3 0 2 1]} {[5 5 7 11 7 10 4 14 4 8]}

Input Arguments

collapse all

Filename of an existing HDF4 or HDF-EOS2 file, specified as a string scalar or character vector.

Depending on the location of your file, filename can take one of these forms.

Location Form
Current folder or folder on the MATLAB® path If the file is in the current folder or in a folder on the MATLAB path, then specify the name of the file infilename.Example: "myFile.hdf"
Other folders If the file is neither in the current folder nor in a folder on the MATLAB path, then specify the full or relative path infilename.Example: "C:\myFolder\myFile.hdf"Example: "myFolder\myFile.hdf"

Name of an HDF SD dataset, specified as a string scalar or character vector.

Example: "mySDDataset"

Name of an HDF V dataset, specified as a string scalar or character vector.

Example: "myVDataset"

Name of an HDF-EOS Grid dataset, specified as a string scalar or character vector.

Example: "myGridDataset"

Name of an HDF-EOS Point dataset, specified as a string scalar or character vector.

Example: "myPointDataset"

Name of an HDF-EOS Swath dataset, specified as a string scalar or character vector.

Example: "mySwathDataset"

Dataset to read, specified as a structure. You can use the hdfinfo function to generate a suitable S structure. If the generated structure contains more than one dataset, indicate which dataset you want the hdfread function to read by specifying the field that relates to a particular type of dataset and using indexing.

Example: S

Example: S.SDS(2)

Name of a 24-bit raster image, specified as a string scalar or character vector.

Example: "myIDataset24Bit"

Name of an 8-bit raster image, specified as a string scalar or character vector.

Example: "myIDataset8Bit"

Names of the HDF V or HDF-EOS fields to read, specified as a string array, character vector, or cell array of character vectors.

Note

You must specify fields for HDF-EOS datasets. However, specifying fields is optional for HDF V datasets.

Example: "myGridField"

Example: ["myField1" "myField2"]

Example: {'myField1' 'myField2'}

Level from which to read an HDF-EOS Point dataset, specified as a string scalar, character vector, or positive integer. You can specify the name of the level using a string scalar or character vector, or you can specify the index of the level using a positive integer.

Example: "myLevel"

Example: 1

Data Types: string | char | double

Name-Value Arguments

collapse all

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: hdfread("example.hdf","Example Vdata",FirstRecord=2,NumRecords=5) reads five records from the Example Vdata V dataset in the file example.hdf, beginning with the second record.

Location, range, and values of the SD, Grid, or Swath dataset to read, specified as a three-element cell vector of positive integer vectors. SpecifyIndex using the form {start stride count}. Each of start, stride, andcount must have type double.

Each nonempty entry of Index must have a number of entries equal to the number of dimensions in the dataset to be read.

Note

If you specify Index for Grid data, then you cannot specifyInterpolate, Pixels, orTile.

If you specify Index for Swath data, then you cannot specify Time.

Example: {[1 10] [3 2] [2 4]}

Example: {[2 4 5] [] []}

Index of the first V record to read, specified as a positive integer.

By default, the hdfread function starts at the first record.

Example: 2

Data Types: double

Number of V records to read, specified as a positive integer.

By default, the hdfread function reads all the records.

Example: 10

Data Types: double

Region of the Grid dataset to read for bilinear interpolation, specified as a two-element cell vector of numeric vectors. Specify Interpolate using the form {lon lat}. Each of lon andlat must have type double.

The hdfread function performs bilinear interpolation on the region described by the lon andlat vectors.

Note

If you specify Interpolate, then you cannot specifyIndex, Pixels, orTile.

Example: {-125:10:-65 25:2.5:50}

Region of the Grid dataset to read, specified as a two-element cell vector of numeric vectors. Specify Pixels using the form {lon lat}. Each of lon and lat must have type double.

The hdfread function reads data from the region described by the lon and lat vectors and converts this region into pixel rows and columns with the origin in the upper-left corner of the grid.

Note

If you specify Pixels, then you cannot specifyIndex, Interpolate, orTile.

Example: {-125:10:-65 25:2.5:50}

Coordinates of one tile in the Grid dataset to read, specified as a numeric vector.

Note

If you specify Tile, then you cannot specifyIndex, Interpolate, orPixels.

Example: [1 1 9]

Data Types: double

Region of the Grid, Point, or Swath dataset to read, specified as a two- or three-element cell vector.

For Grid and Point datasets, specify Box using the form{[lonStart lonStop] [latStart latStop]}; for Swath datasets, specify Box using the form {[lonStart lonStop] [latStart latStop] mode}.

Note

If you specify Box for Point data, then you cannot specifyTime or RecordNumbers.

Example: {[-125 -65] [25 50]}

Example: {[-125 -65] [25 50] "anypoint"}

Time box in the Grid, Point, or Swath dataset to read, specified as a two- or three-element cell vector.

For Grid and Point datasets, specify Time using the form{start stop}; for Swath datasets, specifyTime using the form {start stop mode}.

Note

If you specify Time for Point data, then you cannot specifyBox or RecordNumbers.

If you specify Time for Swath data, then you cannot specifyIndex.

Example: {5 13}

Example: {5 13 "midpoint"}

Portion of the field or dimension of the Grid or Swath dataset to read, specified as a two-element cell vector. Specify Vertical using the form{name range}.

Note

You can specify Vertical alone or withBox or Time.

To subset a region along multiple dimensions, you can specifyVertical up to eight times in a single call tohdfread.

Example: {"myField" [1 5]}

Example: {"myDimension" [11 20]}

Record numbers of the Point dataset to read, specified as a numeric vector.

Note

If you specify RecordNumbers, then you cannot specifyBox or Time.

Example: 10:20

Data Types: double

Output Arguments

collapse all

Data from the dataset, returned as a numeric array, text, or cell array.

Colormap associated with the 8-bit raster image data, returned as a three-column numeric matrix.

More About

collapse all

This table is a quick reference for valid argument combinations for thehdfread function.

Name-Value Argument HDF SD HDF V HDF-EOS Grid HDF-EOS Point HDF-EOS Swath
Fields ✔1 ✔1 ✔1
Level ✔1
Index ✔2 ✔2
FirstRecord
NumRecords
Interpolate ✔2
Pixels ✔2
Tile ✔2
Box ✔2
Time ✔2 ✔2
Vertical
RecordNumbers ✔2

1 — These name-value arguments are required for their respective types of data.

2 — These name-value arguments are mutually exclusive for their respective types of data.

Version History

Introduced before R2006a