Read .mat files in Python

2019-01-01 04:43发布

问题:

Does anyone have successful experience reading 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 installed scipy version 0.7.0, and I can\'t find the loadmat() method)

回答1:

Silly me. Forgot to import io...

import scipy.io
mat = scipy.io.loadmat(\'file.mat\')


回答2:

Neither scipy.io.savemat, nor scipy.io.loadmat work for matlab arrays --v7.3. But the good part is that matlab --v7.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 numpy array


回答3:

I\'ve screwed half an hour even after reading the answers. Hope this answer helps

First save the mat file as

save(\'test.mat\',\'-v7\')

After that in Python use the usual loadmat

import scipy.io as sio
test = sio.loadmat(\'test.mat\')


回答4:

Having Matlab 2014b or newer installed, the Matlab engine for Python could be used:

import matlab.engine
eng = matlab.engine.start_matlab()
content = eng.load(\"example.mat\",nargout=1)


回答5:

There is a nice package called mat4py which can easily be installed using

pip install mat4py

It is straightforward to use (from the website):

Load data from MAT-file

The function loadmat loads all variables stored in the MAT-file into a simple Python data structure, using only Python’s dict and list objects. Numeric and cell arrays are converted to row-ordered nested lists. Arrays are squeezed to eliminate arrays with only one element. The resulting data structure is composed of simple types that are compatible with the JSON format.

Example: Load a MAT-file into a Python data structure:

data = loadmat(\'datafile.mat\')

The variable data is a dict with the variables and values contained in the MAT-file.

Save Python data structure to a MAT-file

Python data can be saved to a MAT-file, with the function savemat. Data has to be structured in the same way as for loadmat, i.e. it should be composed of simple data types, like dict, list, str, int and float.

Example: Save a Python data structure to a MAT-file:

savemat(\'datafile.mat\', data)

The parameter data shall be a dict with the variables.



回答6:

There is also the MATLAB Engine for Python by MathWorks itself. If you have Matlab, this might be worth considered (I haven\'t tried it myself but it has a lot more functionality than just reading Matlab files). However, I don\'t know if it is allowed to distribute it to other users (probably no problem if those persons have Matlab, otherwise maybe NumPy is the right way to go?).

Also, if you want to do all the basics yourself, MathWorks provides (if the link changes, try to google for matfile_format.pdf or its title MAT-FILE Format) a detailed documentation on the structure of the file format. It\'s not as complicated as I personally thought but obviously, this is not the easiest way to go. It also depends on, how many features of the .mat-files you want to support.

I\'ve written a \"small\" (about 700 lines) Python script which can read some basic .mat-files. I\'m neither a Python expert nor a beginner and it took me about two days to write it (using the MathWorks documentation linked above). I\'ve learned a lot of new stuff and it was quite fun (most of the time). As I\'ve written the Python script at work, I\'m afraid I cannot publish it... But I can give a few advices here:

  • First read the documentation
  • Use a HEX-Editor (such as HxD) and look into a reference .mat-file you want to parse
  • Try to figure out the meaning of each Byte by saving the Bytes to a txt-file and annotate each line
  • Use classes to save each data element (such as miCOMPRESSED, miMATRIX, mxDOUBLE or miINT32)
  • The .mat-files\' structure is optimal for saving the data elements in a tree data structure; each node has one class and subnodes


回答7:

Reading the file

import scipy.io
mat = scipy.io.loadmat(file_name)

Insecting the type of mat variable

print(type(mat))
#OUTPUT - <class \'dict\'>

The keys inside the dictionary are matlab variables and the values are the objects assigned to those variables.



回答8:

for high dimensional data, mat4py package works better:

from mat4py import loadmat
data = loadmat(\'datafile.mat\')