How to extract data from he5 files

조회 수: 10 (최근 30일)
Vaidehi Joshi
Vaidehi Joshi 2022년 2월 28일
답변: Manish 2024년 10월 9일
I have downloaded the data of MLS which are he5 format. I want to extract data from the files. I am handling .hdf files for the first time. Could anyone suggest how to extract data from multiple .he5 files?
Any help would be appreciated.
I attached 4 files here in a zip format.

답변 (1개)

Manish
Manish 2024년 10월 9일
Hi,
I understand that you want to extract the data from the multiple he5 files.
The code below reads all. he5 files in a specified directory, extracts datasets into a structure with valid field names, and saves the entire structure as a single .mat file named ‘all_datasets.mat’.
It recursively explores groups and datasets within each. he5 file, ensuring that all data is consolidated into one file for easy access.
To achieve the task, replace your path of the extracted file in the ‘filedir’ variable code below:
fileDir = 'C:\\Users\\Test_files';
fileList = dir(fullfile(fileDir, '*.he5'));
allData = struct();
for k = 1:length(fileList)
fileName = fullfile(fileDir, fileList(k).name);
% Display the file being processed
%disp(['Processing file: ', fileList(k).name]);
fileInfo = h5info(fileName);
% Recursively explore groups and datasets
allData = exploreGroups(fileName, fileInfo.Groups, allData, fileList(k).name);
end
save('all_datasets.mat', '-struct', 'allData')
function allData = exploreGroups(fileName, groups, allData, baseFileName)
for i = 1:length(groups)
disp(['Group: ', groups(i).Name]);
% Explore datasets within the current group
for j = 1:length(groups(i).Datasets)
% Correctly format the dataset path with leading '/'
datasetName = ['/' groups(i).Name, '/', groups(i).Datasets(j).Name];
datasetName = strrep(datasetName, '//', '/'); % Remove any double slashes
disp(['Dataset: ', datasetName]);
try
% Attempt to read the dataset
data = h5read(fileName, datasetName);
% Create a valid field name for the structure
fieldName = matlab.lang.makeValidName([baseFileName, '_', groups(i).Datasets(j).Name]);
% Add the data to the structure
allData.(fieldName) = data;
catch ME
disp(['Error reading dataset: ', ME.message]);
end
end
% Recursively explore subgroups
if ~isempty(groups(i).Groups)
allData = exploreGroups(fileName, groups(i).Groups, allData, baseFileName); % Recursive call
end
end
end
Hope this solves!

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by