reading v 7.3 mat file in python

Solution 1:

Try using h5py module

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

Solution 2:

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!

Solution 3:

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!

Solution 4:

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

Solution 5:

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'])