2016-07-05 165 views
7

有沒有一種標準的方法來轉換matlab.mat(matlab格式化數據)文件到熊貓DataFramematlab數據文件熊貓DataFrame

我知道解決方法是使用scipy.io,但我想知道是否有一種簡單的方法來做到這一點。

回答

8

我找到了2種方法:scipy或mat4py。從MAT文件

  1. mat4py

加載數據

功能loadmat加載存儲在MAT-文件轉換成 簡單的Python數據結構中的所有變量,只使用Python的字典和列表 對象。數字和單元格數組被轉換爲行排序的嵌套列表 。數組被壓縮以消除只有一個元素的數組。 生成的數據結構由與JSON格式兼容的簡單類型 組成。

實施例:加載MAT文件轉換爲Python數據結構:

data = loadmat('datafile.mat') 

來自:

https://pypi.python.org/pypi/mat4py/0.1.0

  • SciPy的:
  • 例如:

    import numpy as np 
    from scipy.io import loadmat # this is the SciPy module that loads mat-files 
    import matplotlib.pyplot as plt 
    from datetime import datetime, date, time 
    import pandas as pd 
    
    mat = loadmat('measured_data.mat') # load mat-file 
    mdata = mat['measuredData'] # variable in mat file 
    mdtype = mdata.dtype # dtypes of structures are "unsized objects" 
    # * SciPy reads in structures as structured NumPy arrays of dtype object 
    # * The size of the array is the size of the structure array, not the number 
    # elements in any particular field. The shape defaults to 2-dimensional. 
    # * For convenience make a dictionary of the data using the names from dtypes 
    # * Since the structure has only one element, but is 2-D, index it at [0, 0] 
    ndata = {n: mdata[n][0, 0] for n in mdtype.names} 
    # Reconstruct the columns of the data table from just the time series 
    # Use the number of intervals to test if a field is a column or metadata 
    columns = [n for n, v in ndata.iteritems() if v.size == ndata['numIntervals']] 
    # now make a data frame, setting the time stamps as the index 
    df = pd.DataFrame(np.concatenate([ndata[c] for c in columns], axis=1), 
            index=[datetime(*ts) for ts in ndata['timestamps']], 
            columns=columns) 
    

    來源:

    http://poquitopicante.blogspot.fr/2014/05/loading-matlab-mat-file-into-pandas.html

  • 最後,你可以使用PyHogs但仍使用SciPy的:
  • 閱讀複雜.mat文件。

    這款筆記本展示了一個讀取Matlab .mat文件的例子, 將數據轉換爲帶有循環的可用字典,一個簡單的圖 的數據。

    http://pyhogs.github.io/reading-mat-files.html

    +0

    的最佳解決方案。這必須選擇答案。 – Jack

    4

    方法可以做到這一點:
    正如你所說SciPy的

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

    使用matlab engine

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