【问题标题】:Errors create when loading matlab files into python将matlab文件加载到python时产生错误
【发布时间】:2019-12-03 16:03:54
【问题描述】:

我在将.mat 文件加载到 Python 中时遇到了一些问题。它是一个结构化数组,有许多主标题和副标题等。使用两个不同的模块导入文件时出现两个错误;

res = scipy.io.loadmat('myfile.mat')

给出以下内容:

return self._matrix_reader.array_from_header(header, process)

文件“mio5_utils.pyx”,第 675 行,在 scipy.io.matlab.mio5_utils.VarReader5.array_from_header

文件“mio5_utils.pyx”,第 723 行,在 scipy.io.matlab.mio5_utils.VarReader5.array_from_header

文件“mio5_utils.pyx”,第 978 行,在 scipy.io.matlab.mio5_utils.VarReader5.read_struct

文件“mio5_utils.pyx”,第 673 行,在 scipy.io.matlab.mio5_utils.VarReader5.read_mi_matrix

文件“mio5_utils.pyx”,第 723 行,在 scipy.io.matlab.mio5_utils.VarReader5.array_from_header

文件“mio5_utils.pyx”,第 978 行,在 scipy.io.matlab.mio5_utils.VarReader5.read_struct

文件“mio5_utils.pyx”,第 673 行,在 scipy.io.matlab.mio5_utils.VarReader5.read_mi_matrix

文件“mio5_utils.pyx”,第 721 行,在 scipy.io.matlab.mio5_utils.VarReader5.array_from_header

文件“mio5_utils.pyx”,第 894 行,在 scipy.io.matlab.mio5_utils.VarReader5.read_cells

文件“mio5_utils.pyx”,第 673 行,在 scipy.io.matlab.mio5_utils.VarReader5.read_mi_matrix

文件“mio5_utils.pyx”,第 721 行,在 scipy.io.matlab.mio5_utils.VarReader5.array_from_header

文件“mio5_utils.pyx”,第 894 行,在 scipy.io.matlab.mio5_utils.VarReader5.read_cells

文件“mio5_utils.pyx”,第 673 行,在 scipy.io.matlab.mio5_utils.VarReader5.read_mi_matrix

文件“mio5_utils.pyx”,第 717 行,在 scipy.io.matlab.mio5_utils.VarReader5.array_from_header

文件“mio5_utils.pyx”,第 879 行,在 scipy.io.matlab.mio5_utils.VarReader5.read_char

TypeError: 缓冲区对于请求的数组来说太小

当使用mat4py 模块时,我得到了

ParseError:意外的字段名称长度:48

关于解决此类问题的任何想法

【问题讨论】:

  • 是否使用 -v7.3 标志保存大数据?如果是这样,您无法使用 scipy 导入它
  • 我不确定它是否有 7.3 标志,但它是一个包含大量大数据的结构数组
  • PS,是否有替代模块可用于将其导入 python
  • 没有简单的解决方案。您可以将拆分的数据保存为更小的部分,将其保存为较大但不在结构中,或者从 python 调用 Matlab 引擎。
  • 感谢您的建议,唯一的问题是我不想使用 Matlab 引擎,因为我希望能够将其发送给没有许可证的人。

标签: python matlab file file-conversion


【解决方案1】:

如果它与-v7.3 标志一起保存,您可以使用h5py 库。 编辑以展示如何遍历带有结构、单元格、数组等的 matfile。

import h5py
from numpy import ndarray

def explore_v73_matfile(file_name=None, fptr=None, path=None):
    if file_name is not None:
        # open the file
        fptr = h5py.File(file_name)
        explore_v73_matfile(None, fptr, ["/"])
        return
    # walk the file tree. not super efficient if very nested, BUT
    # it is very difficult to address if there is a mix of char and
    # int subscripts (nested cells/structs etc)
    o = fptr[path[0]]
    for p in path[1:]:
        o = o[p]
    if isinstance(o, (h5py._hl.group.Group, h5py._hl.files.File)):
        for k in o.keys():
            if (k == "#refs#"):
                # nothing we need is stored under this key
                continue
            else:
                explore_v73_matfile(None, fptr, path + [k])
    elif isinstance(o, h5py._hl.dataset.Dataset):
        if (o.dtype == "|O"):
            # should work for 1D, 2D, ... ND
            for cell in range(o.shape[0]):
                # MATLAB cell array
                explore_v73_matfile(None, fptr, path + [cell])
        else:
            # probably numeric, maybe char (check dtype)
            # this is where you'd find your numeric data arrays 
            print("/".join(map(str,path[1:])))
            print(o[:])
    elif isinstance(o, ndarray):
        # we're walking through a cell or struct array
        for cell in range(len(o)):
            explore_v73_matfile(None, fptr, path + [cell])
    elif isinstance(o, h5py.h5r.Reference):
        # sometimes structs get linked elsewhere if you stuff them in cells

        # print here because the full address gets replaced by [o]
        print("/".join(map(str,path[1:])))
        explore_v73_matfile(None, fptr, [o])
    else:
        raise TypeError("Undefined Behavior. Check MAT File")

【讨论】:

  • 这似乎工作得很好,我已经用标志重新保存了原始文件,现在将它加载到 python 中。我对 h5py 很陌生,所以我想知道如何将结构数组中的 转换为我需要的数据。再次感谢这个建议,真的很有帮助
  • @Questioner 我今晚不在我的机器旁,但我会在明天早上用一些数据/文件/对象访问示例编辑我的答案。
  • 非常感谢,非常感谢您的时间和帮助
  • 您好,我想我已经解决了从对象获取 np.array 的问题。
  • 嗨,彼得,根据我的一些研究,我以稍微(可能不太优化)的方式解决了这个问题:
猜你喜欢
  • 1970-01-01
  • 2019-01-04
  • 2012-09-04
  • 1970-01-01
  • 2020-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多