在 Python 中读取 .mat 文件

发布于 2024-07-21 07:06:53 字数 141 浏览 8 评论 0原文

是否可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

月依秋水 2024-07-28 07:06:53

需要导入,import scipy.io...

import scipy.io
mat = scipy.io.loadmat('file.mat')

An import is required, import scipy.io...

import scipy.io
mat = scipy.io.loadmat('file.mat')
谁的新欢旧爱 2024-07-28 07:06:53

scipy.io.savematscipy.io.loadmat 均不适用于 MATLAB 数组版本 7.3。 但好的一点是 MATLAB 7.3 版文件是 hdf5 数据集。 因此可以使用许多工具来读取它们,包括 NumPy

对于 Python,您将需要 h5py 扩展,这需要您的系统上有 HDF5。

import numpy as np
import h5py
f = h5py.File('somefile.mat','r')
data = f.get('data/variable1')
data = np.array(data) # For converting to a NumPy array

Neither scipy.io.savemat, nor scipy.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.

import numpy as np
import h5py
f = h5py.File('somefile.mat','r')
data = f.get('data/variable1')
data = np.array(data) # For converting to a NumPy array
阳光下慵懒的猫 2024-07-28 07:06:53

首先将 .mat 文件另存为:

save('test.mat', '-v7')

然后,在 Python 中,使用常用的 loadmat 函数:

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

First save the .mat file as:

save('test.mat', '-v7')

After that, in Python, use the usual loadmat function:

import scipy.io as sio
test = sio.loadmat('test.mat')
乖不如嘢 2024-07-28 07:06:53

有一个名为 mat4py 的不错的软件包,可以使用

pip install mat4py

它 轻松安装简单易用(来自网站):

从 MAT 文件加载数据

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

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

from mat4py import loadmat

data = loadmat('datafile.mat')

变量 data 是一个 dict,其中包含 MAT 文件中的变量和值。

将 Python 数据结构保存到 MAT 文件

可以使用函数 savemat 将 Python 数据保存到 MAT 文件。 数据的结构必须与 loadmat 相同,即它应该由简单的数据类型组成,例如 dictlist、<代码>str、intfloat

示例:将 Python 数据结构保存到 MAT 文件:

from mat4py import savemat

savemat('datafile.mat', data)

参数 data 应是带有变量的 dict

There is a nice package called mat4py which can easily be installed using

pip install mat4py

It 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’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.

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

from mat4py import loadmat

data = loadmat('datafile.mat')

The variable data is a dict 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 for loadmat, i.e. it should be composed of simple data types, like dict, list, str, int, and float.

Example: Save a Python data structure to a MAT-file:

from mat4py import savemat

savemat('datafile.mat', data)

The parameter data shall be a dict with the variables.

2024-07-28 07:06:53

有一个很棒的库可以完成此任务,称为:pymatreader

只需执行以下操作:

  1. 安装包:pip install pymatreader

  2. 导入该包的相关函数:from pymatreader import read_mat

  3. 使用函数读取matlab结构体:data = read_mat('matlab_struct.mat')

  4. 使用data.keys()定位数据实际存储的位置.

  • 键通常如下所示:dict_keys(['__header__', '__version__', '__globals__', 'data_opp'])。 其中 data_opp 将是存储数据的实际键。 当然,这个键的名称可以在不同的文件之间更改。
  1. 最后一步 - 创建数据框: my_df = pd.DataFrame(data['data_opp'])

就是这样:)

There is a great library for this task called: pymatreader.

Just do as follows:

  1. Install the package: pip install pymatreader

  2. Import the relevant function of this package: from pymatreader import read_mat

  3. Use the function to read the matlab struct: data = read_mat('matlab_struct.mat')

  4. use data.keys() to locate where the data is actually stored.

  • The keys will usually look like: dict_keys(['__header__', '__version__', '__globals__', 'data_opp']). Where data_opp will be the actual key which stores the data. The name of this key can ofcourse be changed between different files.
  1. Last step - Create your dataframe: my_df = pd.DataFrame(data['data_opp'])

That's it :)

无所谓啦 2024-07-28 07:06:53

安装 MATLAB 2014b 或更高版本后,适用于 Python 的 MATLAB 引擎 可以使用:

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

Having MATLAB 2014b or newer installed, the MATLAB engine for Python could be used:

import matlab.engine
eng = matlab.engine.start_matlab()
content = eng.load("example.mat", nargout=1)
我很坚强 2024-07-28 07:06:53

读取文件

import scipy.io
mat = scipy.io.loadmat(file_name)

检查 MAT 变量的类型

print(type(mat))
#OUTPUT - <class 'dict'>

字典中的MATLAB 变量,并且分配给这些变量的对象

Reading the file

import scipy.io
mat = scipy.io.loadmat(file_name)

Inspecting the type of MAT variable

print(type(mat))
#OUTPUT - <class 'dict'>

The keys inside the dictionary are MATLAB variables, and the values are the objects assigned to those variables.

云巢 2024-07-28 07:06:53

将 mat 文件读取到具有混合数据类型的 pandas dataFrame

import scipy.io as sio
mat=sio.loadmat('file.mat')# load mat-file
mdata = mat['myVar']  # variable in mat file 
ndata = {n: mdata[n][0,0] for n in mdata.dtype.names}
Columns = [n for n, v in ndata.items() if v.size == 1]
d=dict((c, ndata[c][0]) for c in Columns)
df=pd.DataFrame.from_dict(d)
display(df)

To read mat file to pandas dataFrame with mixed data types

import scipy.io as sio
mat=sio.loadmat('file.mat')# load mat-file
mdata = mat['myVar']  # variable in mat file 
ndata = {n: mdata[n][0,0] for n in mdata.dtype.names}
Columns = [n for n, v in ndata.items() if v.size == 1]
d=dict((c, ndata[c][0]) for c in Columns)
df=pd.DataFrame.from_dict(d)
display(df)
意中人 2024-07-28 07:06:53

还有 Python 的 MATLAB 引擎 MathWorks 本身。 如果您有 MATLAB,这可能值得考虑(我自己没有尝试过,但它比仅仅读取 MATLAB 文件有更多的功能)。 但是,我不知道是否允许将其分发给其他用户(如果这些人有 MATLAB,这可能不是问题。否则,也许 NumPy 是正确的方法?)。

另外,如果您想自己完成所有基础知识,MathWorks 提供了(如果链接发生变化,请尝试在 google 上搜索 matfile_format.pdf 或其标题 MAT-FILE Format)有关文件格式结构的详细文档。 它并不像我个人想象的那么复杂,但显然,这不是最简单的方法。 它还取决于您想要支持的 .mat 文件的功能数量。

我编写了一个“小”(大约 700 行)Python 脚本,它可以读取一些基本的 .mat 文件。 我既不是 Python 专家,也不是初学者,我花了大约两天的时间来编写它(使用上面链接的 MathWorks 文档)。 我学到了很多新东西,而且非常有趣(大多数时候)。 由于我在工作中编写了Python脚本,我担心我无法发布它......但我可以在这里给出一些建议:

  • 首先阅读文档。
  • 使用十六进制编辑器(例如 HxD)并查看参考 .mat< /code>-您要解析的文件。
  • 通过将字节保存到 .txt 文件并注释每一行来尝试弄清楚每个字节的含义。
  • 使用类保存每个数据元素(例如 miCOMPRESSEDmiMATRIXmxDOUBLEmiINT32
  • ) >.mat-files 的结构最适合将数据元素保存在树形数据结构中; 每个节点有一个类和子节点

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 title MAT-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:

  • First read the documentation.
  • Use a hex editor (such as HxD) and look into a reference .mat-file you want to parse.
  • Try to figure out the meaning of each byte by saving the bytes to a .txt file and annotate each line.
  • Use classes to save each data element (such as miCOMPRESSED, miMATRIX, mxDOUBLE, or miINT32)
  • The .mat-files' structure is optimal for saving the data elements in a tree data structure; each node has one class and subnodes
荒岛晴空 2024-07-28 07:06:53

除了 v4(1.0 级)、v6、v7 到 7.2 matfile 的 scipy.io.loadmat 和 7.3 格式 matfile 的 h5py.File 之外,还有另一种类型的 matfile 文本数据格式而不是二进制,通常由Octave创建,甚至无法在 MATLAB 中读取

scipy.io.loadmath5py.File 都无法加载它们(在 scipy 1.5.3 和 h5py 3.1.0 上测试),也是我找到的唯一解决方案是numpy.loadtxt

import numpy as np
mat = np.loadtxt('xxx.mat')

Apart from scipy.io.loadmat for v4 (Level 1.0), v6, v7 to 7.2 matfiles and h5py.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 and h5py.File can't load them (tested on scipy 1.5.3 and h5py 3.1.0), and the only solution I found is numpy.loadtxt.

import numpy as np
mat = np.loadtxt('xxx.mat')
娇俏 2024-07-28 07:06:53
  1. 安装 scipy

    pip 安装 scipy

  2. 导入 scipy.io.loadmat 模块

     from scipy.io import loadmat
      annots = loadmat('annotation_0001.mat')
      print(annots)
  1. 解析 .mat 文件结构
   con_list = [[element for element in upperElement] for upperElement in annots['obj_contour']]
  1. 使用 Pandas 数据帧来处理数据
import pandas as pd
   newData = list(zip(con_list[0], con_list[1]))
   columns = ['obj_contour_x', 'obj_contour_y']
   df = pd.DataFrame(newData, columns=columns)

引用:
https://www.askpython.com/python/examples/mat- python 中的文件

  1. Install scipy

    pip install scipy

  2. Import the scipy.io.loadmat module

     from scipy.io import loadmat
      annots = loadmat('annotation_0001.mat')
      print(annots)
  1. Parse the .mat file structure
   con_list = [[element for element in upperElement] for upperElement in annots['obj_contour']]
  1. Use Pandas dataframes to work with the data
import pandas as pd
   newData = list(zip(con_list[0], con_list[1]))
   columns = ['obj_contour_x', 'obj_contour_y']
   df = pd.DataFrame(newData, columns=columns)

refrence:
https://www.askpython.com/python/examples/mat-files-in-python

下雨或天晴 2024-07-28 07:06:53

还可以使用hdf5storage库。 官方文档此处了解有关 matlab 版本的详细信息支持。

import hdf5storage

label_file = "./LabelTrain.mat"
out = hdf5storage.loadmat(label_file) 

print(type(out)) # <class 'dict'>

Can also use the hdf5storage library. official documentation here for details on matlab version support.

import hdf5storage

label_file = "./LabelTrain.mat"
out = hdf5storage.loadmat(label_file) 

print(type(out)) # <class 'dict'>
小梨窩很甜 2024-07-28 07:06:53
from os.path import dirname, join as pjoin
import scipy.io as sio
data_dir = pjoin(dirname(sio.__file__), 'matlab', 'tests', 'data')
mat_fname = pjoin(data_dir, 'testdouble_7.4_GLNX86.mat')
mat_contents = sio.loadmat(mat_fname)

您可以使用上面的代码读取Python中默认保存的.mat文件。

from os.path import dirname, join as pjoin
import scipy.io as sio
data_dir = pjoin(dirname(sio.__file__), 'matlab', 'tests', 'data')
mat_fname = pjoin(data_dir, 'testdouble_7.4_GLNX86.mat')
mat_contents = sio.loadmat(mat_fname)

You can use above code to read the default saved .mat file in Python.

大姐,你呐 2024-07-28 07:06:53

在我自己努力解决这个问题并尝试其他库(我不得不说 mat4py 也是一个很好的库,但有一些限制)之后,我构建了这个库(“matdata2py") 可以处理大多数变量类型,对我来说最重要的是“字符串”类型。 -V7.3版本需要保存.mat文件。 我希望这对社区有用。

安装:

pip install matdata2py

如何使用该库:

import matdata2py as mtp

加载Matlab数据文件:

Variables_output = mtp.loadmatfile(file_Name, StructsExportLikeMatlab = True, ExportVar2PyEnv = False)
print(Variables_output.keys()) # with ExportVar2PyEnv = False the variables are as elements of the Variables_output dictionary. 

使用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:

pip install matdata2py

How to use this lib:

import matdata2py as mtp

To load the Matlab data file:

Variables_output = mtp.loadmatfile(file_Name, StructsExportLikeMatlab = True, ExportVar2PyEnv = False)
print(Variables_output.keys()) # with ExportVar2PyEnv = False the variables are as elements of the Variables_output dictionary. 

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)

鸩远一方 2024-07-28 07:06:53

scipy 将完美地加载 .mat 文件。
我们可以使用 get() 函数将其转换为 numpy 数组。

mat = scipy.io.loadmat('point05m_matrix.mat')

x = mat.get("matrix")
print(type(x))
print(len(x))

plt.imshow(x, extent=[0,60,0,55], aspect='auto')
plt.show()

scipy will work perfectly to load the .mat files.
And we can use the get() function to convert it to a numpy array.

mat = scipy.io.loadmat('point05m_matrix.mat')

x = mat.get("matrix")
print(type(x))
print(len(x))

plt.imshow(x, extent=[0,60,0,55], aspect='auto')
plt.show()
停滞 2024-07-28 07:06:53

在 python 中上传和读取 mat 文件

  1. 在 python 中安装 mat4py。安装成功后,我们得到:
  2. 已成功安装 mat4py-0.5.0。
  3. 从 mat4py 导入 loadmat。
  4. 将文件的实际位置保存在变量内。
  5. 使用 python 将 mat 文件格式加载为数据值
    pip 安装 mat4py
    从 mat4py 导入 loadmat
    波士顿 = r"E:\Downloads\boston.mat"
    data = loadmat(波士顿,meta=False)

To Upload and Read mat files in python

  1. Install mat4py in python.On successful installation we get:
  2. Successfully installed mat4py-0.5.0.
  3. Importing loadmat from mat4py.
  4. Save file actual location inside a variable.
  5. Load mat file format to a data value using python
    pip install mat4py
    from mat4py import loadmat
    boston = r"E:\Downloads\boston.mat"
    data = loadmat(boston, meta=False)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文