필터 지우기
필터 지우기

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 CenterFile Exchange에서 HDF5에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by