3

I would like to know how to put a line break in below HDF5 attribute string.

DescType_id = H5T.copy ('H5T_C_S1');
H5T.set_size (DescType_id, numel(description));
H5T.set_strpad(DescType_id,'H5T_STR_NULLTERM');
DescAttr_id = H5A.create (g2id, 'description', DescType_id, ...
    STimeSpace, 'H5P_DEFAULT');
H5A.write (DescAttr_id, DescType_id, description);
H5T.close(DescType_id);
H5A.close(DescAttr_id);

My description variable would be:

description="Experiment:\nID: 1234\nLocation: London"

I am expecting the line break character to be something like '\n' in above code. Expected Output:

Group '/' 
  Group '/G1' 
    Attributes:
      'description':  'Experiment:
      ID: 1234
      Location: London'

Your help is greatly appreciated. Thanks.

1 Answers1

4

You should set description to the correct string:

description = sprintf('Experiment:\nID: 1234\nLocation: London');

One should note that the usual escape sequences are actually interpreted by the I/O functions (i.e. fprintf, sprintf) and not by the MATLAB language parser. That means, for example, that the literal '\n' (in MATLAB) is a char array of two chars, a backslash and an n, while in C the literal "\n" is a const char array of two chars, one being a new-line, and the other being the string terminator NUL.

A non-portable but "equivalent" way of writing the above description would be concatenating the sub-strings with the line separators:

description = ['Experiment:' char(10) 'ID: 1234' char(10) 'Location: London'];

where char(10) is the char that has the UTF-8 code 10, which happens to be the new-line character.