Borrar filtros
Borrar filtros

How do you read an h5 enum attribute using h5readatt?

10 visualizaciones (últimos 30 días)
Kevin
Kevin el 8 de Abr. de 2024
Comentada: Kevin el 10 de Abr. de 2024
I have an existing h5 file that stores booleans as an enum of 'TRUE' or 'FALSE'. If I try and do an h5readatt(fname,ds,attr) it returns an empty set (no errors). According to documentation I would have expected it to return a string.
How do I use h5readatt() to read enum types? Or is there some other mechanism?

Respuesta aceptada

Shubham
Shubham el 10 de Abr. de 2024
Hi Kevin,
Reading enum types from an HDF5 file in MATLAB can indeed be tricky, especially when the data doesn't come out as expected using h5readatt for attributes or h5read for datasets. The behavior you're experiencing with getting an empty set when trying to read an enum type (like your booleans stored as 'TRUE' or 'FALSE') is not uncommon.
MATLAB's direct HDF5 reading functions (h5read, h5readatt, etc.) often handle simple numeric and string types well but can stumble on more complex types like enums directly out of the box. This is partly because the HDF5 format is very flexible and supports a wide range of data types that MATLAB might not automatically convert into its native types without some additional information or steps.
Workaround for Reading Enum Types
Since h5readatt and h5read might not directly return enums in the form you expect, you may need to use lower-level functions provided by MATLAB to interact with the HDF5 file. These functions give you more control but require a bit more work. Here's a general approach to read enum attributes:
  1. Open the File and the Dataset: Use H5F.open to open the file and H5D.open to open the dataset if you're reading a dataset attribute.
  2. Open the Attribute: Use H5A.open_name to open the attribute by name.
  3. Read the Attribute: Use H5A.read to read the attribute. This step might still not convert the enum to MATLAB strings directly.
  4. Manually Convert Enum Values: Since enums are essentially integers under the hood, you might get integer values that correspond to the enum cases. You would need to manually map these integers back to the strings ('TRUE', 'FALSE') based on your understanding of how they are stored.
Here is an example of how you might approach this:
% Open the file
file_id = H5F.open('yourfile.h5', 'H5F_ACC_RDONLY', 'H5P_DEFAULT');
% Open the dataset if necessary (skip if reading a file attribute)
dataset_id = H5D.open(file_id, '/yourDatasetPath');
% Open the attribute
attribute_id = H5A.open_name(dataset_id, 'yourAttributeName');
% Read the attribute
raw_data = H5A.read(attribute_id, 'H5ML_DEFAULT');
% Close the attribute, dataset, and file
H5A.close(attribute_id);
H5D.close(dataset_id);
H5F.close(file_id);
% Convert raw_data (which might be integers) to your enum strings
% This step depends on how the enums are represented in the file.
% You might need a manual mapping like:
% if raw_data == 0
% value = 'FALSE';
% elseif raw_data == 1
% value = 'TRUE';
% end
Remember, this is a more manual and lower-level approach, requiring you to know a bit about how enums are represented in your specific HDF5 file (e.g., which integer corresponds to 'TRUE' and 'FALSE'). Unfortunately, there's no one-size-fits-all solution due to the flexibility of the HDF5 format and the way MATLAB handles these types.
If you have access to the tool or code that generated the HDF5 file, reviewing how the enums were written might provide insights into how to best read them back into MATLAB.
I hope this helps!
  1 comentario
Kevin
Kevin el 10 de Abr. de 2024
Shubham,
Thanks for the detailed response. I was of course hoping to keep things at the high-level routines, but your examples give me a good head start. I was able to read several different enum attributes and get expected results.
Thanks again,
Kevin

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by