matlab数据文件到pandas DataFrame

Ram*_*nez 13 python database matlab pandas

有没有将matlab .mat(matlab格式化数据)文件转换为Panda 的标准方法DataFrame

我知道通过使用可以解决方法,scipy.io但我想知道是否有一种直接的方法来做到这一点.

Des*_*rif 22

我找到了2路:scipy或mat4py.

  1. mat4py

从MAT文件加载数据

函数loadmat只使用Python的dict和list对象将存储在MAT文件中的所有变量加载到一个简单的Python数据结构中.数字和单元格数组将转换为行排序的嵌套列表.挤压数组以消除只有一个元素的数组.生成的数据结构由与JSON格式兼容的简单类型组成.

示例:将MAT文件加载到Python数据结构中:

data = loadmat('datafile.mat')
Run Code Online (Sandbox Code Playgroud)

从:

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

  1. 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)
Run Code Online (Sandbox Code Playgroud)

从:

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

  1. 最后你可以使用PyHogs但仍然使用scipy:

阅读复杂的.mat文件.

这个笔记本显示了一个读取Matlab .mat文件的示例,将数据转换为带循环的可用字典,这是一个简单的数据图.

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

  • `scipy.io` 和 `mat4py` 模块无法读取 Matlab v7.3+ HDF5 数据文件。 (2认同)
  • 对于 Python3,使用 ndata.items() 而不是 ndata.iteritems() (2认同)

Ser*_*Dev 7

如何做到这一点:
正如你提到的scipy

import scipy.io as sio
test = sio.loadmat('test.mat')
Run Code Online (Sandbox Code Playgroud)

使用matlab引擎:

import matlab.engine
eng = matlab.engine.start_matlab()
content = eng.load("example.mat",nargout=1)
Run Code Online (Sandbox Code Playgroud)