How to save mat files as h5 files?
25 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
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.
- https://www.mathworks.com/help/matlab/ref/h5create.html
- https://www.mathworks.com/help/matlab/ref/h5write.html
Hope it solves your query.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!