フィルターのクリア

Read HDF5 files with groups and subgroups.

51 ビュー (過去 30 日間)
Sebastian Hützen
Sebastian Hützen 2021 年 11 月 22 日
回答済み: Samay Sagar 2024 年 2 月 29 日
Hey,
I have a HDF5 file with some groups and each of these groups contains a number of subgroups.
Is there a way to read all subgroups and save them in some normal arrays or so?

回答 (1 件)

Samay Sagar
Samay Sagar 2024 年 2 月 29 日
You can use the ”h5info” function to get information about the file structure of your input HDF5 file. Thereafter, you can loop through groups and subgroups to read datasets using “h5read” and store them in arrays.
Here’s how you can read HDF5 file to extract groups and subgroups data:
% Specify your HDF5 file name
filename = 'yourfile.hdf5';
% Get info about the HDF5 file
info = h5info(filename);
% Preallocate a cell array to hold your data
data = struct();
% Loop through each group
for i = 1:length(info.Groups)
group_name = info.Groups(i).Name;
data.(matlab.lang.makeValidName(group_name)) = struct(); % Create a struct for the group
% Loop through each subgroup within the group
for j = 1:length(info.Groups(i).Groups)
subgroup_name = info.Groups(i).Groups(j).Name;
data.(matlab.lang.makeValidName(group_name)).(matlab.lang.makeValidName(subgroup_name)) = struct(); % Create a struct for the subgroup
% Loop through each dataset within the subgroup
for k = 1:length(info.Groups(i).Groups(j).Datasets)
dataset_name = info.Groups(i).Groups(j).Datasets(k).Name;
dataset_path = fullfile(subgroup_name, dataset_name);
% Read the dataset and store it in the corresponding struct
data.(matlab.lang.makeValidName(group_name)).(matlab.lang.makeValidName(subgroup_name)).(matlab.lang.makeValidName(dataset_name)) = h5read(filename, dataset_path);
end
end
end
Read more about “h5info” and “h5read” here:

カテゴリ

Help Center および File ExchangeHDF5 についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by