how to convert .mat file to .h5 file

18 views (last 30 days)
XING LIU
XING LIU on 8 Apr 2017
Edited: Manish on 17 Oct 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.

Answers (1)

Manish
Manish on 17 Oct 2024
Edited: Manish on 17 Oct 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!

Categories

Find more on Simulink in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!