Write a string to hdf5 file with defined string length

20 次查看(过去 30 天)
I am trying to write a string to hdf5 file:
h5create('combo.hdf5','/description',1,Datatype='string');
h5write('combo.hdf5','/description',"this is the description");
When I read it out using h5disp, the result is:
Dataset 'description'
Size: 1
MaxSize: 1
Datatype: H5T_STRING
String Length: variable
Padding: H5T_STR_NULLTERM
Character Set: H5T_CSET_UTF8
Character Type: H5T_C_S1
ChunkSize: []
Filters: none
FillValue: ''
Notice the String Length is denoted as 'variable'. What if I want to define the length of this string (23 here), so it shows:
String Length: 23
Thank you very much!

采纳的回答

Amish
Amish 2024-4-5
移动:Walter Roberson 2024-4-25
Hi Kuang,
I see that you want to define the String length to a contant length for the hdf5 file. To define a string with a fixed length when writing to an HDF5 file, you need to specify the string length explicitly when creating the dataset.
However, MATLAB's "h5create" function does not directly allow you to specify the string length for the dataset in its list of parameters. The behavior you're observing, where the string length is set to 'variable', is the default for strings written to HDF5 files through MATLAB's high-level functions.
To create a string dataset with a fixed length, you would typically need to use lower-level functions in a programming language that offers more direct control over HDF5 file operations. Unfortunately, MATLAB's high-level HDF5 functions (like "h5create" and "h5write") are designed for simplicity and do not expose all the underlying HDF5 dataset creation properties, such as fixed string length.
Although, you could try to workaround this issue in MATLAB by treating the string as a dataset of ASCII characters. This approach will involve creating a dataset with a data type of '8-bit integers' (uint8 or int8) and a size that matches the length of your string. The following steps would be involved.
  1. Convert the string to its ASCII numeric representation.
  2. Create a dataset with the appropriate size for the ASCII numeric array.
  3. Write the numeric array to the dataset.
When reading this dataset back into MATLAB, you'll get an array of numeric values. You can convert these back into a character array (string) using "char".
Hope this helps!

更多回答(0 个)

产品


版本

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by