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.


Write data to file in HDF5 format

    Note:   hdf5write will be removed in a future version. Use h5write instead.


hdf5write(filename, details1, dataset1, details2, dataset2,...)
hdf5write(..., 'V71Dimensions', BOOL)


hdf5write(filename,location,dataset) writes the data dataset to the HDF5 file, filename. If filename does not exist, hdf5write creates it. If filename exists, hdf5write overwrites the existing file, by default, but you can also append data to an existing file using an optional syntax.

location defines where to write the data set in the file. HDF5 files are organized in a hierarchical structure similar to a UNIX® directory structure. location is specified as a character vector that resembles a UNIX path.

hdf5write maps the data in dataset to HDF5 data types according to rules outlined below.

hdf5write(filename,details,dataset) writes dataset to filename using the values in the details structure. For a data set, the details structure can contain the following fields.

Field Name


Data Type


Location of the data set in the file

Character array


Name to attach to the data set

Character array

hdf5write(filename,details,attribute) writes the metadata attribute to filename using the values in the details structure. For an attribute, the details structure can contain following fields.

Field Name


Data Type


Location of the object this attribute modifies

Structure array


Identifies what kind of object this attribute modifies; possible values are 'group' and 'dataset'

Character array


Name to attach to the data set

Character array

hdf5write(filename, details1, dataset1, details2, dataset2,...) writes multiple data sets and associated attributes to filename in one operation. Each data set and attribute must have an associated details structure.

hdf5write(filename,...,'WriteMode',mode,...) specifies whether hdf5write overwrites the existing file (the default) or appends data sets and attributes to the file. Possible values for mode are 'overwrite' and 'append'.

hdf5write(..., 'V71Dimensions', BOOL) specifies whether to change the majority of data sets written to the file. If BOOL is true, hdf5write permutes the first two dimensions of the data set, as it did in previous releases (MATLAB® 7.1 [R14SP3] and earlier). This behavior was intended to account for the difference in how HDF5 and MATLAB express array dimensions. HDF5 describes data set dimensions in row-major order; MATLAB stores data in column-major order. However, permuting these dimensions may not correctly reflect the intent of the data and may invalidate metadata. When BOOL is false (the default), the data written to the file correctly reflects the data ordering of the data sets — each dimension in the file's data sets matches the same dimension in the corresponding MATLAB variable.

Data Type Mappings

The following table lists how hdf5write maps the data type from the workspace into an HDF5 file. If the data in the workspace that is being written to the file is a MATLAB data type, hdf5write uses the following rules when translating MATLAB data into HDF5 data objects.

MATLAB Data Type

HDF5 Data Set or Attribute


Corresponding HDF5 native data type. For example, if the workspace data type is uint8, the hdf5write function writes the data to the file as 8-bit integers. The size of the HDF5 dataspace is the same size as the MATLAB array.

Character vector

Single, null-terminated string

Cell array of character vectors

Multiple, null-terminated strings, each the same length. Length is determined by the length of the longest string in the cell array. The size of the HDF5 dataspace is the same size as the cell array.

Cell array of numeric data

Numeric array, the same dimensions as the cell array. The elements of the array must all have the same size and type. The data type is determined by the first element in the cell array.

Structure array

HDF5 compound type. Individual fields in the structure employ the same data translation rules for individual data types. For example, a cell array of strings becomes a multiple, null-terminated strings.

HDF5 objects

If the data being written to the file is composed of HDF5 objects, hdf5write uses the same data type when writing to the file. For all HDF5 objects, except HDF5.h5enum objects, the dataspace has the same dimensions as the array of HDF5 objects passed to the function. For HDF5.h5enum objects, the size and dimensions of the data set in the HDF5 file is the same as the object's Data field.


Write a 5-by-5 data set of uint8 values to the root group.

hdf5write('myfile.h5', '/dataset1', uint8(magic(5)))

Write a 2-by-2 data set of text entries into a subgroup.

dataset = {'north', 'south'; 'east', 'west'};
hdf5write('myfile2.h5', '/group1/dataset1.1', dataset);

Write a data set and attribute to an existing group.

dset = single(rand(10,10));
dset_details.Location = '/group1/dataset1.2';
dset_details.Name = 'Random';

attr = 'Some random data';
attr_details.Name = 'Description';
attr_details.AttachedTo = '/group1/dataset1.2/Random';
attr_details.AttachType = 'dataset';

hdf5write('myfile2.h5', dset_details, dset, ...
           attr_details, attr, 'WriteMode', 'append');

Write a data set using objects.

dset = hdf5.h5array(magic(5));
hdf5write('myfile3.h5', '/g1/objects', dset);

See Also


Introduced before R2006a

Was this topic helpful?