Reading ALL variables in a .mat file with python h5py

后端 未结 1 1570
渐次进展
渐次进展 2020-12-11 11:31

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

相关标签:
1条回答
  • 2020-12-11 11:49

    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
    
    0 讨论(0)
提交回复
热议问题