Main Content


Write HDF5 attribute



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 = 
'03-Mar-2023 06:27:42'

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

File name, specified as a character vector or string scalar containing the name of an existing HDF5 file.

Depending on the location you are writing to, filename can take on one of these forms.



Current folder

To write to the current folder, specify the name of the file in filename.

Example: 'myFile.h5'

Other folders

To write to a folder different from the current folder, specify the full or relative path name in filename.

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

Example: 'myFolder\myFile.h5'

Remote Location

To write to a remote location, 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://bucketname/path_to_file/myFile.h5'

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