【问题标题】:Reading Matlab data in python with scipy.io使用 scipy.io 在 python 中读取 Matlab 数据
【发布时间】:2018-10-29 18:43:36
【问题描述】:

我正在尝试使用 scipy.io 从 MATLAB 5.0 文件中读取一些实验条件。问题是输出文件是一系列复杂得离谱的数组。如何过滤 matlab 文件中的数据?

import scipy.io as sio
with open("sequence_output.txt", "w") as f:
    mat = sio.loadmat("seq_data.seq")
    f.write(str(mat))

这在输出文件中给了我类似下面的内容。 (实际文件大于 800 行)。

如何从这个文件中挑选出我需要的数据?

{'__header__': b'MATLAB 5.0 MAT-file, Platform: PCWIN, Created on: Thu Mar 15 13:50:48 2018', '__version__': '1.0', '__globals__': [], 'StepData': array([[(array([[1]], dtype=uint8), array(['LoadPlate'], dtype='<U9'), array([[1]], dtype=uint8), array([[1]], dtype=uint8), array([[0]], dtype=uint8), array([[0]], dtype=uint8), array([[(array([[12]], dtype=uint8), array([[8]], dtype=uint8), array([[0]], dtype=uint8), array([[1]], dtype=uint8), array([[60]], dtype=uint8), array([[1]], dtype=uint8), array([[1]], dtype=uint8), array([[(array([[1]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[1]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[1]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[1]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[1]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[1]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[1]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[1]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[0]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[0]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[0]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[0]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[0]], dtype=uint8), array([[0]], dtype=uint8)),
        (array([[0]], dtype=uint8), array([[0]], dtype=uint8)),

【问题讨论】:

  • 潜在有用的数据存在以下格式:dtype=[('Selected', 'O'), ('Value', 'O')]), array(['13-Mar-2018'], dtype='&lt;U11'), array(['MG2'], dtype='&lt;U3'), array(['15-Mar-2018'], dtype='&lt;U11'), array([[(array(['M2=Kcv-09-03b'], dtype='&lt;U13'), array([], dtype='&lt;U1'), array([[1]], dtype=uint8), array([[8]], dtype=uint8)), (array(['M2=Kcv-09-03&amp;Kir3.4T'], dtype='&lt;U20'), array([], dtype='&lt;U1'), array([[25]], dtype=uint8), array([[38]], dtype=uint8)),

标签: python arrays matlab scipy


【解决方案1】:

mat['StepData'] 似乎是一个二维数组,包含一个字符串 dtype 数组和一大堆 uint8 数组(都是 (1,1) 数组)。这个数组的shape 可能是(1,n) 但显示不完整。 dtypeobject,因为它包含数组作为元素。 MATLAB 源代码可能是 cell

我无法复制粘贴您的评论以进一步解读它,但它看起来也像是字符串数组和 uint8 的混合体。

MATLAB 矩阵作为 numpy 数值或字符串数​​组加载。它们将始终为 2d(或更高),并且可以转置(或 order='F')。

MATLAB 单元格可以包含混合内容(数字、字符串等),并作为对象 dtype 数组加载。

MATLAB struct 以结构化数组的形式加载,字段名称对应于 struct 字段/属性。

在“[scipy] loadmat”上搜索以查看有关使用 loadmat 的其他问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-26
    • 2018-11-04
    • 1970-01-01
    • 2014-02-06
    • 2021-07-03
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多