Export to HDF4 Files
Write MATLAB® Data to HDF4 File
Write MATLAB arrays to a scientific dataset in an HDF4 file.
Add Namespace to Import List
Add the matlab.io.hdf4.*
path to the import list.
import matlab.io.hdf4.*
Prefix subsequent calls to functions in the matlat.io.hdf4.sd
namespace with sd
, rather than the entire namespace path.
Create HDF4 File
Create a new HDF4 file using the matlab.io.hdf4.sd.start
function. This function corresponds to the SD API function, 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 Dataset
Create a dataset in the file for each MATLAB array you want to export. If you are writing to an existing dataset, you can skip ahead to the next step. In this example, create one dataset for the array of sample data, A
, using the matlab.io.hdf4.sd.create
function. This function corresponds to the SD API function, SDcreate
. The ds_type
argument is a character vector specifying the MATLAB data type of the dataset.
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 dataset identifier, sdsID
.
Write MATLAB Data to HDF4 File
Write data in A
to the dataset in the file using the matlab.io.hdf4.sd.writedata
function. This function corresponds to the SD API function, SDwritedata
. The start
argument specifies the zero-based starting index.
start = [0 0]; sd.writeData(sdsID,start,A);
sd.writeData
queues the write operation. Queued operations execute when you close the HDF4 file.
Write MATLAB Data to Portion of Dataset
Replace the second row of the dataset 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]; sd.writeData(sdsID,start,B);
Write Metadata to HDF4 File
Create a global attribute named creation_date
, with a value that is the current date and time. Use the matlab.io.hdf4.sd.setAttr
function, which corresponds to the SD API function, SDsetattr
.
sd.setAttr(sdID,'creation_date',string(datetime('now')));
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 dataset identified by sdsID
. Possible values of this attribute include the text strings 'cartesian'
, 'polar'
, and 'spherical'
.
attr_name = 'cordsys'; attr_value = 'polar'; sd.setAttr(sdsID,attr_name,attr_value);
Close HDF4 Dataset
Close access to the dataset, using the matlab.io.hdf4.sd.endAccess
function. This function corresponds to the SD API function, SDendaccess
. You must close access to all the datasets in and HDF4 file before closing the file.
sd.endAccess(sdsID);
Close HDF4 File
Close the HDF4 file using the matlab.io.hdf4.sd.close
function. This function corresponds to the SD API function, SDend
.
sd.close(sdID);
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
function 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.
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 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
function as an argument:
hdfml('closeall')
See Also
sd.start
| sd.create
| sd.writeData
| sd.setAttr
| sd.close
| sd.endAccess
| hdfml