Python matlab 数据文件到 Pandas DataFrame

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38197449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:29:40  来源:igfitidea点击:

matlab data file to pandas DataFrame

pythondatabasematlabpandas

提问by Ramon Martinez

Is there a standard way to convert matlab.mat(matlab formated data) files to Panda DataFrame?

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

I am aware that a workaround is possible by using scipy.iobut I am wondering whether there is a straightforward way to do it.

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

回答by Destrif

I found 2 way: scipy or mat4py.

我找到了 2 种方式:scipy 或 mat4py。

  1. mat4py
  1. mat4py

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.

从 MAT 文件加载数据

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

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

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

data = loadmat('datafile.mat')

From:

从:

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

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

  1. Scipy:
  1. 西皮:

Example:

例子:

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)

From:

从:

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

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

  1. Finally you can use PyHogs but still use scipy:
  1. 最后你可以使用 PyHogs 但仍然使用 scipy:

Reading complex .matfiles.

This notebook shows an example of reading a Matlab .mat file, converting the data into a usable dictionary with loops, a simple plot of the data.

读取复杂的.mat文件。

该笔记本显示了读取 Matlab .mat 文件的示例,将数据转换为带有循环的可用字典,即数据的简单绘图。

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

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

回答by SerialDev

Ways to do this:
As you mentioned scipy

这样做的方法:
正如你提到的 scipy

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

Using the matlab engine:

使用matlab引擎

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