将Matlab文件加载到python中时产生错误 [英] Errors create when loading matlab files into python

查看:178
本文介绍了将Matlab文件加载到python中时产生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having some trouble loading a .mat file into Python. It is a structured array with many main and subheadings etc. and I have two errors when using two different modules to import the files;

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

给出以下内容:

返回self._matrix_reader.array_from_header(标头,进程)

return self._matrix_reader.array_from_header(header, process)

文件"mio5_utils.pyx",行675,在scipy.io.matlab.mio5_utils.VarReader5.array_from_header

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

文件"mio5_utils.pyx",行723,在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

文件"mio5_utils.pyx",行978,在scipy.io.matlab.mio5_utils.VarReader5.read_struct

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

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

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

文件"mio5_utils.pyx",行723,在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

文件"mio5_utils.pyx",行978,在scipy.io.matlab.mio5_utils.VarReader5.read_struct

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

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

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

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

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

文件"mio5_utils.pyx",第894行,位于scipy.io.matlab.mio5_utils.VarReader5.read_cells

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

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

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

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

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

文件"mio5_utils.pyx",第894行,位于scipy.io.matlab.mio5_utils.VarReader5.read_cells

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

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

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

文件"mio5_utils.pyx",第717行,位于scipy.io.matlab.mio5_utils.VarReader5.array_from_header

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

文件"mio5_utils.pyx",行879,在scipy.io.matlab.mio5_utils.VarReader5.read_char

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

TypeError:缓冲区对于请求的数组而言太小

TypeError: buffer is too small for requested array

使用 mat4py 模块时,我只会得到

When using the mat4py module I simply get

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

ParseError: Unexpected field name length: 48

解决这种问题的任何想法

Any ideas of a way around such a problem

推荐答案

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

If it is saved with the -v7.3 flag you can use the h5py library. Edited to show how to walk through a matfile with structs, cells, arrays etc.

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")

这篇关于将Matlab文件加载到python中时产生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆