reading v 7.3 mat file in python

2020-01-30 04:01发布

问题:

I am trying to read a matlab file with the following code

import scipy.io
mat = scipy.io.loadmat('test.mat')

and it gives me the following error

raise NotImplementedError('Please use HDF reader for matlab v7.3 files')
NotImplementedError: Please use HDF reader for matlab v7.3 files

so could anyone please had the same problem and could please any sample code

thanks

回答1:

Try using h5py module

import h5py
with h5py.File('test.mat', 'r') as f:
    f.keys()


回答2:

import h5py
import numpy as np
filepath = '/path/to/data.mat'
arrays = {}
f = h5py.File(filepath)
for k, v in f.items():
    arrays[k] = np.array(v)

you should end up with your data in the arrays dict, unless you have MATLAB structures, I suspect. Hope it helps!



回答3:

Per Magu_'s answer on a related thread, check out the package hdf5storage which has convenience functions to read v7.3 matlab mat files; it is as simple as

import hdf5storage
mat = hdf5storage.loadmat('test.mat')


回答4:

I had a look at this issue: https://github.com/h5py/h5py/issues/726. If you saved your mat file with -v7.3 option, you should generate the list of keys with (under Python 3.x):

import h5py
with h5py.File('test.mat', 'r') as file:
    print(list(file.keys()))

In order to access the variable a for instance, you have to use the same trick:

with h5py.File('test.mat', 'r') as file:
    a = list(file['a'])


回答5:

According to the Scipy cookbook. http://wiki.scipy.org/Cookbook/Reading_mat_files,

Beginning at release 7.3 of Matlab, mat files are actually saved using the HDF5 format by default (except if you use the -vX flag at save time, see help save in Matlab). These files can be read in Python using, for instance, the PyTables or h5py package. Reading Matlab structures in mat files does not seem supported at this point.

Perhaps you could use Octave to re-save using the -vX flag.



回答6:

Despite hours of searching I've not found how to access Matlab v7.3 structures either. Hopefully this partial answer will help someone, and I'd be very happy to see extra pointers.

So starting with (I think the [0][0] arises from Matlab giving everything to dimensions):

f = h5py.File('filename', 'r')
f['varname'][0][0]

gives: < HDF5 object reference >

Pass this reference to f again:

f[f['varname'][0][0]]

which gives an array: convert this to a numpy array and extract the value (or, recursively, another < HDF5 object reference > :

np.array(f[f['varname'][0][0]])[0][0]

If accessing the disk is slow, maybe loading to memory would help.


Further edit: after much futile searching my final workaround (I really hope someone else has a better solution!) was calling Matlab from python which is pretty easy and fast:

eng = matlab.engine.start_matlab()  # first fire up a Matlab instance
eng.quit()
eng = matlab.engine.connect_matlab()  # or connect to an existing one
eng.sqrt(4.0)
x = 4.0
eng.workspace['y'] = x
a = eng.eval('sqrt(y)')
print(a)
x = eng.eval('parameterised_function_in_Matlab(1, 1)', nargout=1)
a = eng.eval('Structured_variable{1}{2}.object_name')  # (nested cell, cell, object)


回答7:

This function reads Matlab-produced HDF5 .mat files, and returns a structure of nested dicts of Numpy arrays. Matlab writes matrices in Fortran order, so this also transposes matrices and higher-dimensional arrays into conventional Numpy order arr[..., page, row, col].

import h5py

def read_matlab(filename):
    def conv(path=''):
        p = path or '/'
        paths[p] = ret = {}
        for k, v in f[p].items():
            if type(v).__name__ == 'Group':
                ret[k] = conv(f'{path}/{k}')  # Nested struct
                continue
            v = v[()]  # It's a Numpy array now
            if v.dtype == 'object':
                # HDF5ObjectReferences are converted into a list of actual pointers
                ret[k] = [r and paths.get(f[r].name, f[r].name) for r in v.flat]
            else:
                # Matrices and other numeric arrays
                ret[k] = v if v.ndim < 2 else v.swapaxes(-1, -2)
        return ret

    paths = {}
    with h5py.File(filename, 'r') as f:
        return conv()


回答8:

I've created a small library to load MATLAB 7.3 files:

pip install mat73

To load a .mat 7.3 into Python as a dictionary:

import mat73
data_dict = mat73.loadmat('data.mat')

simple as that!