Find specific value in h5 file

6 views (last 30 days)
Autumn P
Autumn P on 26 Jan 2022
Answered: Riya on 19 Nov 2023
Hi,
I have a bunch of h5 files with storm data that have varying organization. I am trying to find a specific value ('Record Interval'), however it's not always in the same place.
Sometimes it is located within exampleFile.Group.Attributes and sometimes it is located within the exampleFile.Group.Datasets and then within these two locations the final location often varies. I tried doing an if else but it seems to vary too much to find it consistently and I have close to 700 files where I need to get the record interval for over 700K storms.
Is there a way to find the location of 'Record Interval' just by using the name?

Answers (1)

Riya
Riya on 19 Nov 2023
Hello Autumn,
As per my understanding you want to find specific value in h5 file.
Please note that you can use the `h5info` function in MATLAB to recursively search for the 'Record Interval' attribute or dataset within the h5 file. Here's an example of how you can achieve this:
function location = findRecordInterval(filePath)
file = h5info(filePath);
location = recursiveSearch(file, '');
end
function location = recursiveSearch(group, location)
% Check if the 'Record Interval' attribute exists in the current group
attributes = {group.Attributes.Name};
if ismember('Record Interval', attributes)
location = [location '.Attributes'];
return;
end
% Check if the 'Record Interval' dataset exists in the current group
datasets = {group.Datasets.Name};
if ismember('Record Interval', datasets)
location = [location '.Datasets'];
return;
end
% Recursively search subgroups
for i = 1:length(group.Groups)
subgroup = group.Groups(i);
location = recursiveSearch(subgroup, [location '.Groups(' num2str(i) ')']);
if ~isempty(location)
return;
end
end
% If 'Record Interval' is not found, return an empty location
location = '';
end
To use this code, you can call the `findRecordInterval` function with the path to your h5 file as an input. It will return the location of the 'Record Interval' attribute or dataset within the file. The location will be a string indicating the path to the attribute or dataset, such as `.Attributes` or `.Datasets`.
Note that this code assumes that the 'Record Interval' attribute or dataset is unique within each file. If there are multiple occurrences, it will return the location of the first occurrence found. You can modify the code if you need to handle multiple occurrences differently.
For more information regarding h5info you can refer following article:
I hope it helps.

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!