how to display elements of arrays in a .mat file in python

前端 未结 1 1493
傲寒
傲寒 2020-12-22 14:39

This is the first time that I try to work with \".mat\" files. I am going to use the data of a \".mat\" file, but the elements of the arrays can not be opened. Can

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

    As @hpaulj commented, you need to determine which objects are Groups and which are Datasets. And with Matlab datasets, you need to determine which are arrays and which are objects (objects point to other HDF5 objects in your file). Until you're comfortable with HDF5 and h5py, the easiest way to do this is with the HDFView utility from the HDF Group.

    When you're ready to code, you can do it pragmatically with isinstance() referencing h5py objects.
    To test if variable node is a Group use:

    if isinstance(node, h5py.Group):
    

    To test if variable node is a Dataset use:

    if isinstance(node, h5py.Dataset):
    

    To test if variable node is an Object Dataset use:

    if (node.dtype == 'object') :
    

    You can use visititems(-function-) to loop recursively down an object tree, calling -function- with each object.

    Here's a very simple example to demonstrate. Put your filename in the place of foo.hdf5 and run. Warning: This creates a lot of output if you have a lot of groups and datasets. Once you understand your file schema, you should be able to access the datasets. If you find object datasets, read my linked answer to dereference them.

    import numpy as np
    import h5py
    
    def visitor_func(name, node):
        if isinstance(node, h5py.Group):
            print(node.name, 'is a Group')
        elif isinstance(node, h5py.Dataset):
           if (node.dtype == 'object') :
                print (node.name, 'is an object Dataset')
           else:
                print(node.name, 'is a Dataset')   
        else:
            print(node.name, 'is an unknown type')         
    #########    
    
    print ('testing hdf5 matlab file')
    h5f = h5py.File('foo.hdf5')
    
    h5f.visititems(visitor_func)   
    
    h5f.close()
    
    0 讨论(0)
提交回复
热议问题