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 = 
'29-Feb-2020 00:14:08'

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.

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).

Compatibility Considerations

expand all

Behavior changed in R2020a

Introduced in R2011a