Read .mat files in Python

后端 未结 8 1806
时光说笑
时光说笑 2020-11-22 09:23

Is it possible to read binary MATLAB .mat files in Python?

I\'ve seen that SciPy has alleged support for reading .mat files, but I\'m unsuccessful with it. I install

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 09:42

    Neither scipy.io.savemat, nor scipy.io.loadmat work for MATLAB arrays version 7.3. But the good part is that MATLAB version 7.3 files are hdf5 datasets. So they can be read using a number of tools, including NumPy.

    For Python, you will need the h5py extension, which requires HDF5 on your system.

    import numpy as np
    import h5py
    f = h5py.File('somefile.mat','r')
    data = f.get('data/variable1')
    data = np.array(data) # For converting to a NumPy array
    

提交回复
热议问题