1

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!

hpaulj
  • 221,503
  • 14
  • 230
  • 353
Tom
  • 531
  • 4
  • 7
  • 19
  • We've explored this in previous SO questions. But in the mean time, explore the data structure of the file. Look at `f.keys()` (add `list(...)` in py3). You may have to work through multiple layers. Also read the basics of using `h5py`. – hpaulj Sep 04 '17 at 23:21
  • From the sidebar: https://stackoverflow.com/questions/19310808/how-to-read-a-v7-3-mat-file-via-h5py?rq=1 and https://stackoverflow.com/questions/27670149/read-matlab-v7-3-file-into-python-list-of-numpy-arrays-via-h5py – hpaulj Sep 04 '17 at 23:23

1 Answers1

2

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

hpaulj
  • 221,503
  • 14
  • 230
  • 353