This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Export to HDF4 Files

Write MATLAB Data to HDF4 File

This example shows how to write MATLAB® arrays to a Scientific Data Set in an HDF4 file.

Add Package to Import List

Add the* path to the import list.


Prefix subsequent calls to functions in the package with sd, rather than the entire package path.

Create HDF4 File

Create a new HDF4 file using the function. This function corresponds to the SD API routine, SDstart.

sdID = sd.start('mydata.hdf','create');

sd.start creates the file and returns a file identifier named sdID.

To open an existing file instead of creating a new one, call sd.start with 'write' access instead of 'create'.

Create HDF4 Data Set

Create a data set in the file for each MATLAB array you want to export. If you are writing to an existing data set, you can skip ahead to the next step. In this example, create one data set for the array of sample data, A, using the function. This function corresponds to the SD API routine, SDcreate. The ds_type argument is a string specifying the MATLAB data type of the data set.

A = [1 2 3 4 5 ; 6 7 8 9 10 ; 11 12 13 14 15];
ds_name = 'A';
ds_type = 'double';
ds_dims = size(A);
sdsID = sd.create(sdID,ds_name,ds_type,ds_dims);

sd.create returns an HDF4 SD data set identifier, sdsID.

Write MATLAB Data to HDF4 File

Write data in A to the data set in the file using the function. This function corresponds to the SD API routine, SDwritedata. The start argument specifies the zero-based starting index.

start = [0 0];

sd.writeData queues the write operation. Queued operations execute when you close the HDF4 file.

Write MATLAB Data to Portion of Data Set

Replace the second row of the data set with the vector B. Use a start input value of [1 0] to begin writing at the second row, first column. start uses zero-based indexing.

B = [9 9 9 9 9];
start = [1 0];

Write Metadata to HDF4 File

Create a global attribute named creation_date, with a value that is the current date and time. Use the function, which corresponds to the SD API routine, SDsetattr.


sd.Attr creates a file attribute, also called a global attribute, associated with the HDF4 file identified by sdID.

Associate a predefined attribute, cordsys, to the data set identified by sdsID. Possible values of this attribute include the text strings 'cartesian', 'polar', and 'spherical'.

attr_name = 'cordsys';
attr_value = 'polar';

Close HDF4 Data Set

Close access to the data set, using the function. This function corresponds to the SD API routine, SDendaccess. You must close access to all the data sets in and HDF4 file before closing the file.


Close HDF4 File

Close the HDF4 file using the function. This function corresponds to the SD API routine, SDend.


Closing an HDF4 file executes all the write operations that have been queued using SDwritedata.

Manage HDF4 Identifiers

MATLAB® supports utility functions that make it easier to use HDF4 in the MATLAB environment.

View All Open HDF4 Identifiers

Use the gateway function to the MATLAB HDF4 utility API, hdfml, and specify the name of the listinfo routine as an argument to view all the currently open HDF4 identifiers. MATLAB updates this list whenever HDF identifiers are created or closed. In this example only two identifiers are open.

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:
Open scientific data file identifiers:
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 identifiers

Close All Open HDF4 Identifiers

Close all the currently open HDF4 identifiers in a single call using the gateway function to the MATLAB HDF4 utility API, hdfml. Specify the name of the closeall routine as an argument:


See Also

| | | | | |

More About

Was this topic helpful?