在 Python 中读取 .mat 文件
是否可以在 Python 中读取二进制 MATLAB .mat 文件?
我已经看到 SciPy 声称支持读取 .mat 文件,但我没有成功。 我安装了 SciPy 0.7.0 版本,但找不到 loadmat()
方法。
Is it possible to read binary MATLAB .mat files in Python?
I've seen that SciPy has alleged support for reading .mat files, but I'm unsuccessful with it. I installed SciPy version 0.7.0, and I can't find the loadmat()
method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
需要导入,
import scipy.io
...An import is required,
import scipy.io
...scipy.io.savemat
和scipy.io.loadmat
均不适用于 MATLAB 数组版本 7.3。 但好的一点是 MATLAB 7.3 版文件是 hdf5 数据集。 因此可以使用许多工具来读取它们,包括 NumPy。对于 Python,您将需要
h5py
扩展,这需要您的系统上有 HDF5。Neither
scipy.io.savemat
, norscipy.io.loadmat
work for MATLAB arrays version 7.3. But the good part is that MATLAB version 7.3 files are hdf5 datasets. So they can be read using a number of tools, including NumPy.For Python, you will need the
h5py
extension, which requires HDF5 on your system.首先将 .mat 文件另存为:
然后,在 Python 中,使用常用的
loadmat
函数:First save the .mat file as:
After that, in Python, use the usual
loadmat
function:有一个名为
mat4py
的不错的软件包,可以使用它 轻松安装简单易用(来自网站):
从 MAT 文件加载数据
函数
loadmat
将 MAT 文件中存储的所有变量加载到简单的 Python 数据结构中,使用仅 Python 的dict
和list
对象。 数字和元胞数组将转换为行排序的嵌套列表。 数组被压缩以消除只有一个元素的数组。 生成的数据结构由与 JSON 格式兼容的简单类型组成。示例:将 MAT 文件加载到 Python 数据结构中:
变量
data
是一个dict
,其中包含 MAT 文件中的变量和值。将 Python 数据结构保存到 MAT 文件
可以使用函数
savemat
将 Python 数据保存到 MAT 文件。 数据的结构必须与loadmat
相同,即它应该由简单的数据类型组成,例如dict
、list
、<代码>str、int
和float
。示例:将 Python 数据结构保存到 MAT 文件:
参数
data
应是带有变量的dict
。There is a nice package called
mat4py
which can easily be installed usingIt is straightforward to use (from the website):
Load data from a MAT-file
The function
loadmat
loads all variables stored in the MAT-file into a simple Python data structure, using only Python’sdict
andlist
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.Example: Load a MAT-file into a Python data structure:
The variable
data
is adict
with the variables and values contained in the MAT-file.Save a Python data structure to a MAT-file
Python data can be saved to a MAT-file, with the function
savemat
. Data has to be structured in the same way as forloadmat
, i.e. it should be composed of simple data types, likedict
,list
,str
,int
, andfloat
.Example: Save a Python data structure to a MAT-file:
The parameter
data
shall be adict
with the variables.有一个很棒的库可以完成此任务,称为:
pymatreader
。只需执行以下操作:
安装包:
pip install pymatreader
导入该包的相关函数:
from pymatreader import read_mat
使用函数读取matlab结构体:
data = read_mat('matlab_struct.mat')
使用
data.keys()
定位数据实际存储的位置.dict_keys(['__header__', '__version__', '__globals__', 'data_opp'])
。 其中data_opp
将是存储数据的实际键。 当然,这个键的名称可以在不同的文件之间更改。my_df = pd.DataFrame(data['data_opp'])
就是这样:)
There is a great library for this task called:
pymatreader
.Just do as follows:
Install the package:
pip install pymatreader
Import the relevant function of this package:
from pymatreader import read_mat
Use the function to read the matlab struct:
data = read_mat('matlab_struct.mat')
use
data.keys()
to locate where the data is actually stored.dict_keys(['__header__', '__version__', '__globals__', 'data_opp'])
. Wheredata_opp
will be the actual key which stores the data. The name of this key can ofcourse be changed between different files.my_df = pd.DataFrame(data['data_opp'])
That's it :)
安装 MATLAB 2014b 或更高版本后,适用于 Python 的 MATLAB 引擎 可以使用:
Having MATLAB 2014b or newer installed, the MATLAB engine for Python could be used:
读取文件
检查 MAT 变量的类型
字典中的键是MATLAB 变量,并且值是分配给这些变量的对象。
Reading the file
Inspecting the type of MAT variable
The keys inside the dictionary are MATLAB variables, and the values are the objects assigned to those variables.
将 mat 文件读取到具有混合数据类型的 pandas dataFrame
To read mat file to pandas dataFrame with mixed data types
还有 Python 的 MATLAB 引擎 MathWorks 本身。 如果您有 MATLAB,这可能值得考虑(我自己没有尝试过,但它比仅仅读取 MATLAB 文件有更多的功能)。 但是,我不知道是否允许将其分发给其他用户(如果这些人有 MATLAB,这可能不是问题。否则,也许 NumPy 是正确的方法?)。
另外,如果您想自己完成所有基础知识,MathWorks 提供了(如果链接发生变化,请尝试在 google 上搜索
matfile_format.pdf
或其标题MAT-FILE Format
)有关文件格式结构的详细文档。 它并不像我个人想象的那么复杂,但显然,这不是最简单的方法。 它还取决于您想要支持的.mat
文件的功能数量。我编写了一个“小”(大约 700 行)Python 脚本,它可以读取一些基本的 .mat 文件。 我既不是 Python 专家,也不是初学者,我花了大约两天的时间来编写它(使用上面链接的 MathWorks 文档)。 我学到了很多新东西,而且非常有趣(大多数时候)。 由于我在工作中编写了Python脚本,我担心我无法发布它......但我可以在这里给出一些建议:
.mat< /code>-您要解析的文件。
miCOMPRESSED
、miMATRIX
、mxDOUBLE
或miINT32
There is also the MATLAB Engine for Python by MathWorks itself. If you have MATLAB, this might be worth considering (I haven't tried it myself but it has a lot more functionality than just reading MATLAB files). However, I don't know if it is allowed to distribute it to other users (it is probably not a problem if those persons have MATLAB. Otherwise, maybe NumPy is the right way to go?).
Also, if you want to do all the basics yourself, MathWorks provides (if the link changes, try to google for
matfile_format.pdf
or its titleMAT-FILE Format
) a detailed documentation on the structure of the file format. It's not as complicated as I personally thought, but obviously, this is not the easiest way to go. It also depends on how many features of the.mat
-files you want to support.I've written a "small" (about 700 lines) Python script which can read some basic
.mat
-files. I'm neither a Python expert nor a beginner and it took me about two days to write it (using the MathWorks documentation linked above). I've learned a lot of new stuff and it was quite fun (most of the time). As I've written the Python script at work, I'm afraid I cannot publish it... But I can give some advice here:.mat
-file you want to parse.miCOMPRESSED
,miMATRIX
,mxDOUBLE
, ormiINT32
).mat
-files' structure is optimal for saving the data elements in a tree data structure; each node has one class and subnodes除了 v4(1.0 级)、v6、v7 到 7.2 matfile 的 scipy.io.loadmat 和 7.3 格式 matfile 的 h5py.File 之外,还有另一种类型的 matfile 文本数据格式而不是二进制,通常由Octave创建,甚至无法在 MATLAB 中读取。
scipy.io.loadmat
和h5py.File
都无法加载它们(在 scipy 1.5.3 和 h5py 3.1.0 上测试),也是我找到的唯一解决方案是numpy.loadtxt
。Apart from
scipy.io.loadmat
for v4 (Level 1.0), v6, v7 to 7.2 matfiles andh5py.File
for 7.3 format matfiles, there is anther type of matfiles in text data format instead of binary, usually created by Octave, which can't even be read in MATLAB.Both of
scipy.io.loadmat
andh5py.File
can't load them (tested on scipy 1.5.3 and h5py 3.1.0), and the only solution I found isnumpy.loadtxt
.安装 scipy
pip 安装 scipy
导入 scipy.io.loadmat 模块
引用:
https://www.askpython.com/python/examples/mat- python 中的文件
Install scipy
pip install scipy
Import the scipy.io.loadmat module
refrence:
https://www.askpython.com/python/examples/mat-files-in-python
还可以使用hdf5storage库。 官方文档此处了解有关 matlab 版本的详细信息支持。
Can also use the hdf5storage library. official documentation here for details on matlab version support.
您可以使用上面的代码读取Python中默认保存的.mat文件。
You can use above code to read the default saved .mat file in Python.
在我自己努力解决这个问题并尝试其他库(我不得不说 mat4py 也是一个很好的库,但有一些限制)之后,我构建了这个库(“matdata2py") 可以处理大多数变量类型,对我来说最重要的是“字符串”类型。 -V7.3版本需要保存.mat文件。 我希望这对社区有用。
安装:
如何使用该库:
加载Matlab数据文件:
使用ExportVar2PyEnv = True,您可以将每个变量分别视为Python变量,其名称与Mat文件中保存的名称相同。
标志说明
StructsExportLikeMatlab = True/False 结构以字典格式 (False) 或类似于 Matlab 的点格式导出 (True)
ExportVar2PyEnv = True/False 将所有变量导出到单个字典中 (True) 或作为单独的单个变量导出到 python 中环境(错误)
After struggling with this problem myself and trying other libraries (I have to say mat4py is a good one as well but with a few limitations) I have built this library ("matdata2py") that can handle most variable types and most importantly for me the "string" type. The .mat file needs to be saved in the -V7.3 version. I hope this can be useful for the community.
Installation:
How to use this lib:
To load the Matlab data file:
with ExportVar2PyEnv = True you can see each variable separately as python variables with the same name as saved in the Mat file.
Flag descriptions
StructsExportLikeMatlab = True/False structures are exported in dictionary format (False) or dot-based format similar to Matlab (True)
ExportVar2PyEnv = True/False export all variables in a single dictionary (True) or as separate individual variables into the python environment (False)
scipy 将完美地加载 .mat 文件。
我们可以使用 get() 函数将其转换为 numpy 数组。
scipy will work perfectly to load the .mat files.
And we can use the get() function to convert it to a numpy array.
在 python 中上传和读取 mat 文件
pip 安装 mat4py
从 mat4py 导入 loadmat
波士顿 = r"E:\Downloads\boston.mat"
data = loadmat(波士顿,meta=False)
To Upload and Read mat files in python
pip install mat4py
from mat4py import loadmat
boston = r"E:\Downloads\boston.mat"
data = loadmat(boston, meta=False)