How to save mat files as h5 files?
23 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Abhaya
on 10 Oct 2024
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 Comments
See Also
Categories
Find more on Data Type Conversion 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!