Read .mat files in Python

后端 未结 8 1604
时光说笑
时光说笑 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:45

    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 a 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:

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

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

    Save a 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:

    from mat4py import savemat
    
    savemat('datafile.mat', data)
    

    The parameter data shall be a dict with the variables.

提交回复
热议问题