Find specific value in h5 file
5 views (last 30 days)
Show older comments
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?
0 Comments
Answers (1)
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.
0 Comments
See Also
Categories
Find more on Workspace Variables and MAT-Files 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!