【问题标题】:matlab data file to pandas DataFrame [duplicate]matlab数据文件到pandas DataFrame [重复]
【发布时间】:2016-11-06 22:29:58
【问题描述】:

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

我知道使用scipy.io 可以解决问题,但我想知道是否有直接的方法。

【问题讨论】:

  • @MarkMikofski 我不认为这是“在 Python 中读取 .mat 文件”的副本,它没有涉及如何处理提取的数据以便可以将其放入 Pandas 数据框中。

标签: python database matlab pandas


【解决方案1】:

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

  1. mat4py

从 MAT 文件中加载数据

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

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

data = loadmat('datafile.mat')

发件人:

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)

发件人:

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.iomat4py 模块无法读取 Matlab v7.3+ HDF5 数据文件。
  • 对于 Python3,使用 ndata.items() 代替 ndata.iteritems()
  • mat4py 的已知限制: * 超过 2 维的数组 [重要] * 复数数组 [重要] * 稀疏数组 [重要] * 函数数组 * 对象类 * 匿名函数类 pypi.org/project/mat4py
【解决方案2】:

方法:
正如你提到的 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)

【讨论】:

  • Matlab 说“你不能在只有 MATLAB Runtime 的机器上运行 MATLAB 引擎”uk.mathworks.com/help/matlab/…
  • @SuhasC 您安装了不支持开发(无头)的 MATLAB 版本。您需要重新安装 MATLAB,并选择安装开发者扩展(头文件)的任何选项。
猜你喜欢
  • 1970-01-01
  • 2019-03-28
  • 2018-01-09
  • 2017-10-27
  • 1970-01-01
  • 2020-11-01
  • 2020-03-30
  • 2020-11-13
  • 1970-01-01
相关资源
最近更新 更多