matlab数据文件到pandas DataFrame [重复]

2024-04-04

有没有标准的转换方法matlab .mat(matlab格式数据)文件到PandaDataFrame?

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


我找到了两种方法:scipy 或 mat4py。

  1. mat4py

从 MAT 文件加载数据

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

示例:将 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:

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. 最后你可以使用 PyHogs 但仍然使用 scipy:

阅读综合体.mat files.

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

matlab数据文件到pandas DataFrame [重复] 的相关文章

随机推荐