How to save mat files as h5 files?

25 次查看(过去 30 天)
Gordon
Gordon 2022-11-18
回答: Abhaya 2024-10-10
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 个评论
Gordon
Gordon 2022-11-20
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.

请先登录,再进行评论。

回答(1 个)

Abhaya
Abhaya 2024-10-10
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.

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by