How to save mat files as h5 files?

26 visualizaciones (últimos 30 días)
Gordon
Gordon el 18 de Nov. de 2022
Respondida: Abhaya el 10 de Oct. de 2024
I have cell arrays saved as a .mat file and I want to transform this into a .h5 file.
How do I transform a .mat file into a .h5 file?
I have read that we should try to use hdf5write or h5write to do so, but I do not understand how to use these functions.
I am trying to use a code written by a colleague that processes data as .h5 files , but does not do so with .mat files, unfortunately my data is all in .mat files.
  2 comentarios
Gordon
Gordon el 20 de Nov. de 2022
Thankyou for your help. My problem is that I do not understand how to correctly use the h5read and h5create functions.
I have attempted loading my file, but because it is a 1x1 struct with 70 fields, I don't know what to do.

Iniciar sesión para comentar.

Respuestas (1)

Abhaya
Abhaya el 10 de Oct. de 2024
Hi Gordon,
I understand that you want to convert a 1x1 struct with 70 fields, to an H5 file using ‘h5write’ function. However, the ‘h5write’ function does not inherently support writing structs directly into H5 files.
To save the struct into an H5 file, you will need to manually iterate over each field in the struct and write each one as a separate dataset in the HDF5 file.
Please refer to the following sample code for better understanding.
%Simple struct with two fields
dataStruct = struct();
dataStruct.field1 = rand(3);
dataStruct.field2 = [1, 2, 3, 4, 5];
h5filename = 'dataStruct.h5';
% Get the field names of the struct
fields = fieldnames(dataStruct);
% Loop over each field in the struct
for i = 1:length(fields)
fieldName = fields{i};
fieldData = dataStruct.(fieldName); % Access the data in the field
% Determine the size of the field data
dataSize = size(fieldData);
% Create a dataset for each struct field and write into it
h5create(h5filename, ['/' fieldName], dataSize);
h5write(h5filename, ['/' fieldName], fieldData);
end
For more information, please refer to the MATLAB documentations linked below.
Hope it solves your query.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by