how to import .mat-v7.3 file using h5py

有些话、适合烂在心里 提交于 2019-12-20 04:51:09

问题


I have .mat file which have 3 matrixes A, B, C.

Actually I used scipy.io to import this mat file as below.

data = sio.loadmat('/data.mat')
A = data['A']
B = data['B']
C = data['C']

But, v7.3 file cannot import using this way. So, I tried to import using h5py but I don't know how to use h5py. My code is as below.

f = h5py.File('/data.mat', 'r')
A = f.get('/A')
A = np.array('A')

Which part is wrong? Thank you!


回答1:


In Octave

>> A = [1,2,3;4,5,6];
>> B = [1,2,3,4];
>> save -hdf5 abc.h5 A B

In Ipython

In [138]: import h5py
In [139]: f = h5py.File('abc.h5')
In [140]: list(f.keys())
Out[140]: ['A', 'B']
In [141]: list(f['A'].keys())
Out[141]: ['type', 'value']
In [142]: f['A']['value']
Out[142]: <HDF5 dataset "value": shape (3, 2), type "<f8">
In [143]: A = f['A']['value'][:]
In [144]: A
Out[144]: 
array([[ 1.,  4.],
       [ 2.,  5.],
       [ 3.,  6.]])

See also links in the sidebar.

Basically it's a matter of finding the desired dataset, and then loading it as described in http://docs.h5py.org/en/latest/high/dataset.html#reading-writing-data

https://pypi.python.org/pypi/hdf5storage/0.1.14 - this package has MATLAB MAT v7.3 file support. I haven't used it yet.


In [550]: import hdf5storage
In [560]: bar = hdf5storage.read(filename='abc.h5')
In [561]: bar
Out[561]: 
array([ ([(b'matrix', [[ 1.,  4.], [ 2.,  5.], [ 3.,  6.]])], [(b'matrix', [[ 1.], [ 2.], [ 3.], [ 4.]])])],
      dtype=[('A', [('type', 'S7'), ('value', '<f8', (3, 2))], (1,)), ('B', [('type', 'S7'), ('value', '<f8', (4, 1))], (1,))])

So the file has been loaded as a structured array, with shape (1,) and 2 fields, 'A' and 'B' (the 2 variable names). Each in turn has a 'type' and 'value' field.

In [565]: bar['A']['value']
Out[565]: 
array([[[[ 1.,  4.],
         [ 2.,  5.],
         [ 3.,  6.]]]])

Or using its loadmat:

In [570]: out = hdf5storage.loadmat('abc.h5',appendmat=False)
In [571]: out
Out[571]: 
{'A': array([(b'matrix', [[ 1.,  4.], [ 2.,  5.], [ 3.,  6.]])],
       dtype=[('type', 'S7'), ('value', '<f8', (3, 2))]),
 'B': array([(b'matrix', [[ 1.], [ 2.], [ 3.], [ 4.]])],
       dtype=[('type', 'S7'), ('value', '<f8', (4, 1))])}

out is a dictionary:

In [572]: out['B']['value']
Out[572]: 
array([[[ 1.],
        [ 2.],
        [ 3.],
        [ 4.]]])

For reading a simple MATLAB file this doesn't add much. It may add more with cells or structs. But for writing a MATLAB compatible file it should be a big help (though for writing one could stick with scipy.io.savemat).



来源:https://stackoverflow.com/questions/46044613/how-to-import-mat-v7-3-file-using-h5py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!