3

I'm trying to pull in all the variables from a '.mat' v7.3 file, and turn them into NumPy arrays. Is there a way to do this generically, preferably not needing to specify variable names? How can you get all present variable names from a h5py.File, then check their dimensions?

Ex.

 import numpy as np, h5py

 file = h5py.File('data.mat','r')
 for "all variables in mat file"
     ...fill numpy array
 end
CodyF
  • 4,977
  • 3
  • 26
  • 38
  • 1
    Have you explored this file in an interactive session? `h5py` is self documenting, allowing you to find the `keys` of groups and datasets, and iterating over the same. In other words, for a start, treat the file like you would any other unknown `h5` file. – hpaulj Apr 24 '15 at 16:24
  • Previous questions on reading v7.3 via h5py http://stackoverflow.com/questions/27670149/read-matlab-v7-3-file-into-python-list-of-numpy-arrays-via-h5py http://stackoverflow.com/questions/19310808/how-to-read-a-v7-3-mat-file-via-h5py/27699851#27699851 – hpaulj Apr 24 '15 at 19:05

1 Answers1

8

After seeing some of the comments, and the documentations for H5PY Groups, I've found that you can iterate through all of the H5PY "Items" to get the value associated to each variable name. I gave an example below. Please post if their is a better way of grabbing the variable names and values.

Note: The example only pulls the value of variables that contain numeric arrays (h5py.Dataset). If you have nested Groups or cell arrays then you need to access them further to get the values.

import numpy as np
import h5py

f = h5py.File('simdata_020_01.mat','r')
variables = f.items()

for var in variables:
    name = var[0]
    data = var[1]
    print "Name ", name  # Name
    if type(data) is h5py.Dataset:
        # If DataSet pull the associated Data
        # If not a dataset, you may need to access the element sub-items
        value = data.value
        print "Value", value  # NumPy Array / Value
CodyF
  • 4,977
  • 3
  • 26
  • 38
  • 4
    You can just do `for name, data in f.items():` instead of the whole `variables` and `var[0]` thing. – LucasB Jan 06 '17 at 14:43