将 matlab 文件加载到 python 时出错

Errors create when loading matlab files into python

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

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

给出以下内容:

return self._matrix_reader.array_from_header(header, process)

File "mio5_utils.pyx", line 675, in scipy.io.matlab.mio5_utils.VarReader5.array_from_header

File "mio5_utils.pyx", line 723, in scipy.io.matlab.mio5_utils.VarReader5.array_from_header

File "mio5_utils.pyx", line 978, in scipy.io.matlab.mio5_utils.VarReader5.read_struct

File "mio5_utils.pyx", line 673, in scipy.io.matlab.mio5_utils.VarReader5.read_mi_matrix

File "mio5_utils.pyx", line 723, in scipy.io.matlab.mio5_utils.VarReader5.array_from_header

File "mio5_utils.pyx", line 978, in scipy.io.matlab.mio5_utils.VarReader5.read_struct

File "mio5_utils.pyx", line 673, in scipy.io.matlab.mio5_utils.VarReader5.read_mi_matrix

File "mio5_utils.pyx", line 721, in scipy.io.matlab.mio5_utils.VarReader5.array_from_header

File "mio5_utils.pyx", line 894, in scipy.io.matlab.mio5_utils.VarReader5.read_cells

File "mio5_utils.pyx", line 673, in scipy.io.matlab.mio5_utils.VarReader5.read_mi_matrix

File "mio5_utils.pyx", line 721, in scipy.io.matlab.mio5_utils.VarReader5.array_from_header

File "mio5_utils.pyx", line 894, in scipy.io.matlab.mio5_utils.VarReader5.read_cells

File "mio5_utils.pyx", line 673, in scipy.io.matlab.mio5_utils.VarReader5.read_mi_matrix

File "mio5_utils.pyx", line 717, in scipy.io.matlab.mio5_utils.VarReader5.array_from_header

File "mio5_utils.pyx", line 879, in scipy.io.matlab.mio5_utils.VarReader5.read_char

TypeError: buffer is too small for requested array

使用 mat4py 模块时,我只得到

ParseError: Unexpected field name length: 48

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

如果使用 -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")