how to convert .mat file to .h5 file

조회 수: 21 (최근 30일)
XING LIU
XING LIU 2017년 4월 8일
편집: Manish 2024년 10월 17일
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
Goncalo Costa
Goncalo Costa 2022년 11월 21일
Did you find a solution to this problem?

댓글을 달려면 로그인하십시오.

답변 (1개)

Manish
Manish 2024년 10월 17일
편집: Manish 2024년 10월 17일
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!

카테고리

Help CenterFile Exchange에서 Simulink에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by