get_all_data_from_level_h5_rec.m 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. function [data, all_strings] = get_all_data_from_level_h5_rec(filename, str, data, all_strings)
  2. % This function recursively reads data from an HDF5 file.
  3. %
  4. % INPUT:
  5. % filename: A string that identifies the HDF5 file from which to extract data.
  6. % str: The group from which to start extracting data.
  7. % data: A cell array to add and store the extracted data.
  8. % all_strings: A cell array to add and store the paths of the extracted
  9. % datasets (for control)
  10. %
  11. % OUTPUT:
  12. % data: A cell array containing the extracted data for each dataset.
  13. % all_strings: A cell array containing the paths of the extracted datasets.
  14. % authors: Francesco E. Vaccari
  15. % date: 03/2024
  16. % Get information about the HDF5 file.
  17. info = h5info(filename, str);
  18. % If there are groups in the current group...
  19. if ~isempty(info.Groups)
  20. for i = 1:length(info.Groups)
  21. % ...recursively call this function on the group (to dig in the
  22. % hierarchy)
  23. [data, all_strings] = get_all_data_from_level_h5_rec(filename, info.Groups(i).Name, data, all_strings);
  24. end
  25. % If there are no groups but there are datasets in the current group...
  26. elseif isempty(info.Groups) && ~isempty(info.Datasets)
  27. row_num = size(data,1);
  28. for i = 1:length(info.Datasets)
  29. % ...read the data from the dataset and store it in the 'data' cell array.
  30. data{row_num+1,i} = h5read(filename, [info.Name '/' info.Datasets(i).Name]);
  31. all_strings{row_num+1,i} = [info.Name '/' info.Datasets(i).Name];
  32. end
  33. end
  34. end