This is an archival dump of old wiki content --- see scipy.org for current material.
Please see http://scipy-cookbook.readthedocs.org/

Attachment 'h5load.m'

Download

   1 function data=h5load(filename, path)
   2 %
   3 % data = H5LOAD(filename)
   4 % data = H5LOAD(filename, path_in_file)
   5 %
   6 % Load data in a HDF5 file to a Matlab structure.
   7 %
   8 % Parameters
   9 % ----------
  10 %
  11 % filename
  12 %     Name of the file to load data from
  13 % path_in_file : optional
  14 %     Path to the part of the HDF5 file to load
  15 %
  16 
  17 % Author: Pauli Virtanen <pav@iki.fi>
  18 % This script is in the Public Domain. No warranty.
  19 
  20 if nargin > 1
  21   path_parts = regexp(path, '/', 'split');
  22 else
  23   path = '';
  24   path_parts = [];
  25 end
  26 
  27 loc = H5F.open(filename, 'H5F_ACC_RDONLY', 'H5P_DEFAULT');
  28 try
  29   data = load_one(loc, path_parts, path);
  30   H5F.close(loc);
  31 catch exc
  32   H5F.close(loc);
  33   rethrow(exc);
  34 end
  35 
  36 
  37 function data=load_one(loc, path_parts, full_path)
  38 % Load a record recursively.
  39 
  40 while ~isempty(path_parts) & strcmp(path_parts{1}, '')
  41   path_parts = path_parts(2:end);
  42 end
  43 
  44 data = struct();
  45 
  46 num_objs = H5G.get_num_objs(loc);
  47 
  48 % 
  49 % Load groups and datasets
  50 %
  51 for j_item=0:num_objs-1,
  52   objtype = H5G.get_objtype_by_idx(loc, j_item);
  53   objname = H5G.get_objname_by_idx(loc, j_item);
  54   
  55   if objtype == 1
  56     % Group
  57     name = regexprep(objname, '.*/', '');
  58   
  59     if isempty(path_parts) | strcmp(path_parts{1}, name)
  60       if ~isempty(regexp(name,'^[a-zA-Z].*'))
  61 	group_loc = H5G.open(loc, name);
  62 	try
  63 	  sub_data = load_one(group_loc, path_parts(2:end), full_path);
  64 	  H5G.close(group_loc);
  65 	catch exc
  66 	  H5G.close(group_loc);
  67 	  rethrow(exc);
  68 	end
  69 	if isempty(path_parts)
  70 	  data = setfield(data, name, sub_data);
  71 	else
  72 	  data = sub_data;
  73 	  return
  74 	end
  75       end
  76     end
  77    
  78   elseif objtype == 2
  79     % Dataset
  80     name = regexprep(objname, '.*/', '');
  81   
  82     if isempty(path_parts) | strcmp(path_parts{1}, name)
  83       if ~isempty(regexp(name,'^[a-zA-Z].*'))
  84 	dataset_loc = H5D.open(loc, name);
  85 	try
  86 	  sub_data = H5D.read(dataset_loc, ...
  87 	      'H5ML_DEFAULT', 'H5S_ALL','H5S_ALL','H5P_DEFAULT');
  88 	  H5D.close(dataset_loc);
  89 	catch exc
  90 	  H5D.close(dataset_loc);
  91 	  rethrow(exc);
  92 	end
  93 	
  94 	sub_data = fix_data(sub_data);
  95 	
  96 	if isempty(path_parts)
  97 	  data = setfield(data, name, sub_data);
  98 	else
  99 	  data = sub_data;
 100 	  return
 101 	end
 102       end
 103     end
 104   end
 105 end
 106 
 107 % Check that we managed to load something if path walking is in progress
 108 if ~isempty(path_parts)
 109   error(sprintf('Path "%s" not found in the HDF5 file', full_path));
 110 end
 111 
 112 
 113 function data=fix_data(data)
 114 % Fix some common types of data to more friendly form.
 115 
 116 if isstruct(data)
 117   fields = fieldnames(data);
 118   if length(fields) == 2 & strcmp(fields{1}, 'r') & strcmp(fields{2}, 'i')
 119     if isnumeric(data.r) & isnumeric(data.i)
 120       data = data.r + 1j*data.i;
 121     end
 122   end
 123 end
 124 
 125 if isnumeric(data) & ndims(data) > 1
 126   % permute dimensions
 127   data = permute(data, fliplr(1:ndims(data)));
 128 end

New Attachment

File to upload
Rename to
Overwrite existing attachment of same name

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.