Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
804 views
in Technique[技术] by (71.8m points)

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

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 any one help me? Since the "*.mat" file is > 7.3, I can not use Scipy.io

 import numpy as np
 import h5py

 f = h5py.File('data.mat')
 for i in f.keys():
    aa = f[i]
    aa=np.array(aa)
    print i,':','
',aa

When I use aa=np.array(aa)[0], the output would be the name of the f.key(), but I need the elements of the f.key()

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...