Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

read matlab v7.3 file into python list of numpy arrays via h5py

I know this has been asked before but in my opinion there are still no answers that explain what is going on and don't happen to work for my case. I have a matlab v7.3 file that is structured like so,

           ---> rank <1x454 cell>    ---> each element is <53x50 double>
   f.mat
           ---> compare <1x454 cell> ---> each element is <53x50 double>

I hope this is straight forward enough. So what I am trying to do is read all 454 arrays with dimensions 53x54 from the cell array named 'rank', into a list of numpy arrays in python using the h5py library like so:

import h5py

with h5py.File("f.mat") as f:
    data = [np.array(element) for element in f['rank']]

what I end up with is a list of arrays of HDF5 object references:

In [53]: data[0]
Out[53]: array([<HDF5 object reference>], dtype=object)

What do I do with this / how do I get the list of arrays that I need?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Well I found the solution to my problem. If anyone else has a better solution or can better explain I'd still like to hear it.

Basically, the <HDF5 object reference> needed to be used to index the h5py file object to get the underlying array that is being referenced. After we are referring to the array that is needed, it has to be loaded to memory by indexing it with [:] or any subset if only part of the array is required. Here is what I mean:

with h5py.File("f.mat") as f:
    data = [f[element[0]][:] for element in f['rank']]

and the result:

In [79]: data[0].shape
Out[79]: (50L, 53L)

In [80]: data[0].dtype
Out[80]: dtype('float64')

Hope this helps anyone in the future. I think this is the most general solution I've seen so far.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...