h5 file import

조회 수: 43 (최근 30일)
jUNSONG PENGT
jUNSONG PENGT 2015년 8월 26일
편집: Walter Roberson 2018년 12월 26일
I used a code to read a h5 file in matlab. However, there are 2000 different dataset names in the h5 file. To read one of the data, I have to use the code once:
double(h5read(tracename,'/Waveforms/Channel 1/Channel 1 Seg9995Data')
'Channel 1 Seg9995Data' is the name of one of the data. So I have to use this code 2000 times to read the whole data in the h5 file, which is very time consuming. Is there a simple way to do this? Thank you.

답변 (2개)

Walter Roberson
Walter Roberson 2015년 8월 26일
info = h5info(tracename);
trace_datasets = info.Datasets;
dataset_names = {trace_datasets.Name};
for K = 1 : length(dataset_names)
double(h5read(tracename,dataset_names{K}))
end
  댓글 수: 5
per isakson
per isakson 2015년 8월 29일
편집: per isakson 2015년 8월 30일
That depends on the organization of the h5-file. It could be deeply nested, which gives a deeply nested structure, info. I guess, "/Waveforms/Channel 1" indicates a group named, "Channel 1", under a group named, "Waveforms". Furthermore, I guess, the datasets are in the group "Channel 1". (Too much guessing!)
Here is an example with a file I used for testing.
>> info0 = h5info('c:\m\PiaX\xtests\h5test.h5')
info0 =
Filename: 'C:\m\PiaX\xtests\h5test.h5'
Name: '/'
Groups: [2x1 struct]
Datasets: []
Datatypes: []
Links: []
Attributes: []
>>
>> {info0.Datasets.Name}
Attempt to reference field of non-structure array.
>>
>> {info0.Groups.Name}
ans =
'/group01_L1' '/group02_L1'
>>
>>
>> info1 = h5info('c:\m\PiaX\xtests\h5test.h5','/group01_L1')
info1 =
Filename: 'C:\m\PiaX\xtests\h5test.h5'
Name: '/group01_L1'
Groups: []
Datasets: [2x1 struct]
Datatypes: []
Links: []
Attributes: []
>>
>> {info1.Datasets.Name}
ans =
'data11_L2' 'data12_L2'
per isakson
per isakson 2015년 8월 31일
@jUNSONG, What does
info = h5info(tracename)
display in your command window?

댓글을 달려면 로그인하십시오.


Lennart M
Lennart M 2018년 3월 7일
편집: per isakson 2018년 3월 9일
Thanks to this thread, I managed to write a nice import function for my h5 files in MATLAB. I'll post it here for reference, maybe someone can use it. All files matching a file name pattern within a directory are imported and their data is merged into one large MATLAB struct. You may need to modify the code to fit your h5 file structure.
function h5data = loadh5bypattern(folder, filenamePattern)
% scan folder for matching files
filelist = dir(fullfile(folder,filenamePattern));
h5data = struct;
% parse all matching files...
for k = 1:numel(filelist)
file = filelist(k);
if ~file.isdir
filename = filelist(k).name;
fprintf(filename);
info = h5info(filename);
groups = info.Groups;
nGroups = length(groups);
for i=1:nGroups
groupname = groups(i).Name;
cleanname = regexprep(groupname,'[/]','');
field = h5read(filename,strcat(groupname,'/data'));
% the gps position group has some strange fields, remove them
if strcmp(cleanname, 'gps_position')
field = rmfield(field,'altitude_units');
field = rmfield(field,'geo_sep_units');
field = rmfield(field,'lat_dir');
field = rmfield(field,'lon_dir');
end
% this field is not wanted either
if isfield(field,'unit')
field = rmfield(field,'unit');
end
% first time: create new entry, after that append
if ~isfield(h5data, cleanname)
h5data.(cleanname) = field;
else
existingfields = fieldnames(h5data.(cleanname));
for j=1:length(existingfields)
entryname = existingfields{j};
h5data.(cleanname).(entryname) = ...
[h5data.(cleanname).(entryname); field.(entryname)];
end
end
% progress display
if mod(i, floor(nGroups/15)) == 0
fprintf('.');
end
end
fprintf('\n');
end
end
disp('Import of .h5 files succcessful! Storing as .mat file...');
% after successful import, store as .mat file
storename = regexprep(filenamePattern,'[*(.h5)]','');
save(storename,'h5data');
end
  댓글 수: 3
Walter Roberson
Walter Roberson 2018년 10월 23일
"All files matching a file name pattern within a directory are imported"
"filelist = dir(fullfile(folder,filenamePattern));"
So in the first input you should be passing a directory name, as a character vector, and in the second input you should be passing a pattern accepted by your file system, such 'amoeba_*.h5', indicating that all files in the given directory that match the given pattern should be imported.
Shakir Hussain
Shakir Hussain 2018년 12월 26일
편집: Walter Roberson 2018년 12월 26일
HI Lennart
Though this post is three years old but it is useful for me now.
How I can you your code to import, read and merge in single file of matlab a bunch of MODIS snow cover MOD10C2 HDF-EOS https://modis.gsfc.nasa.gov/data/dataprod/mod10.php ?

댓글을 달려면 로그인하십시오.

카테고리

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