how to convert .mat file to .h5 file

18 visualizaciones (últimos 30 días)
XING LIU
XING LIU el 8 de Abr. de 2017
Editada: Manish el 17 de Oct. de 2024
I have a dateset which is saved as .mat files. Now I have to transfer them into .h5 format,but I am a starter and I don't have any experience before,so anyone knows about it and answers me would help me a lot,thanks.
  1 comentario
Goncalo Costa
Goncalo Costa el 21 de Nov. de 2022
Did you find a solution to this problem?

Iniciar sesión para comentar.

Respuestas (1)

Manish
Manish el 17 de Oct. de 2024
Editada: Manish el 17 de Oct. de 2024
Hi,
I understand you want to convert the .mat file to .h5 file.To convert a .mat file to an .h5 file, use the ‘h5create’ function to create the HDF5 file and the ‘h5write’ function to write the data from the .mat file into HDF5 format.
Here is the sample code:
data = load(matFileName);
h5FileName = 'data.h5';
% Loop through each variable in the .mat file
fields = fieldnames(data);
for i = 1:length(fields)
fieldName = fields{i};
fieldData = data.(fieldName);
% Define the dataset path in the .h5 file
datasetPath = ['/' fieldName];
if ischar(fieldData)
% Handle strings: convert to uint8 (ASCII values)
fieldData = uint8(fieldData);
h5create(h5FileName, datasetPath, size(fieldData), 'Datatype', 'uint8');
else
% Create the dataset in the .h5 file
h5create(h5FileName, datasetPath, size(fieldData));
end
h5write(h5FileName, datasetPath, fieldData);
disp(['Converted and wrote variable "', fieldName, '" to ', h5FileName]);
end
Here is the documentation link for ‘h5write’ and ‘h5create’:
Hope this helps!

Categorías

Más información sobre Simulink en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by