h5 file import
43 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
Réponses (2)
Walter Roberson
le 26 Août 2015
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 commentaires
per isakson
le 29 Août 2015
Modifié(e) : per isakson
le 30 Août 2015
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
le 31 Août 2015
@jUNSONG, What does
info = h5info(tracename)
display in your command window?
Lennart M
le 7 Mar 2018
Modifié(e) : per isakson
le 9 Mar 2018
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 commentaires
Walter Roberson
le 23 Oct 2018
"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
le 26 Déc 2018
Modifié(e) : Walter Roberson
le 26 Déc 2018
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 ?
Voir également
Catégories
En savoir plus sur HDF5 dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!