Main Content


Write attribute to HDF5 file



h5writeatt(filename,loc,attr,val) writes the attribute named attr with the value val to the specified location in the HDF5 file.

h5writeatt(filename,loc,attr,val,'TextEncoding',encoding) writes attr to the specified location in the HDF5 file using the specified encoding. 'UTF-8' is the default setting for 'TextEncoding', while 'system' uses the system encoding to represent characters. It is usually unnecessary to specify 'system' as the encoding.


collapse all

Write an attribute to the root group of examplefile.h5 whose value is the current time.

date = datestr(now);
h5writeatt('examplefile.h5','/','creation_date', date);

Read the attribute from the root group of the HDF5 file.

val1 = h5readatt('examplefile.h5','/','creation_date')
val1 = 
'12-Feb-2024 22:51:21'

Create an array of doubles and write it to the dataset /g4/world.

attData = [0 1 2 3];

Display the dataset metadata. The attribute val2 is listed one of the attributes belonging to the dataset.

HDF5 examplefile.h5 
Dataset 'world' 
    Size:  36x19
    MaxSize:  36x19
    Datatype:   H5T_IEEE_F64LE (double)
    ChunkSize:  []
    Filters:  none
    FillValue:  0.000000
        'val2':  0.000000 1.000000 2.000000 3.000000 

Input Arguments

collapse all

Filename of an existing HDF5 file, specified as a string scalar or character vector.

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



Current folder

Specify the name of the file in filename.

Example: "myFile.h5"

Other folders

If the file is not in the current folder or in a folder on the MATLAB® path, then specify the full or relative path in filename.

Example: "C:\myFolder\myFile.h5"

Example: "myFolder\myFile.h5"

Remote location

If the file is stored at a remote location, then filename must contain the full path of the file specified as a uniform resource locator (URL) of the form:


Based on your remote location, scheme_name can be one of the values in this table.

Remote Locationscheme_name
Amazon S3™s3
Windows Azure® Blob Storagewasb, wasbs

For more information, see Work with Remote Data.

Example: "s3://myBucket/myFolder/myFile.h5"

  • If your file consists of several physical files using the Family driver, specify filename using a format specifier. For example, to use the Family driver with the two files family0.h5 and family1.h5, specify filename as "family%d.h5".

  • If your file consists of several physical files using the Multi driver or the Split driver, specify filename as the leading prefix of the names of the physical files. For example, to use the Multi driver with the six files multi-b.h5, multi-g.h5, multi-l.h5, multi-o.h5, multi-r.h5, and multi-s.h5, specify filename as "multi".

Location in file, specified as a character vector or string scalar containing the full path name of an existing group or dataset to which you want to associate the attribute.

Name of attribute, specified as a character vector or string scalar containing the name of an attribute belonging to a group or dataset. If the attribute does not exist, h5writeatt creates the attribute with the name specified.

If the specified attribute already exists but does not have a datatype or dataspace consistent with val, h5writeatt deletes the attribute and recreates it. String attributes are created with a scalar dataspace, which is a single, zero-dimensional data point.

Value of attribute to be written, specified as a character vector, string scalar, or numeric value.

Text encoding, specified as the comma-separated pair consisting of 'TextEncoding' and one of these values:

  • 'UTF-8' — Represents characters using UTF-8 encoding.

  • 'system' — Represents characters as bytes using the system encoding (not recommended).


  • h5writeatt does not support writing to files stored remotely in HDFS™.

Version History

Introduced in R2011a

expand all