ValueError: Unknown mat file type, version 0, 0, KeyError: "Unable to open object (object 'data' doe

Serena ·
更新时间:2024-11-13
· 928 次阅读

python3读取.mat文件:

代码: 

from scipy import io mat_file = r'/home/data/1.mat' io.loadmat(mat_file)

报错:

Traceback (most recent call last):
  File "/home/user1/test.py", line 78, in
    show_mat(mat)
  File "/home/user1/test.py", line 50, in show_mat_flow
    x = io.loadmat(mat_file)
  File "/home/user1/.local/lib/python3.6/site-packages/scipy/io/matlab/mio.py", line 207, in loadmat
    MR, file_opened = mat_reader_factory(file_name, appendmat, **kwargs)
  File "/home/user1/.local/lib/python3.6/site-packages/scipy/io/matlab/mio.py", line 63, in mat_reader_factory
    mjv, mnv = get_matfile_version(byte_stream)
  File "/home/user1/.local/lib/python3.6/site-packages/scipy/io/matlab/miobase.py", line 241, in get_matfile_version
    raise ValueError('Unknown mat file type, version %s, %s' % ret)
ValueError: Unknown mat file type, version 0, 0

还有一种报错:

Traceback (most recent call last):
  File "", line 1, in
  File "/data/user1/pkgs/conda/envs/drc/lib/python3.7/site-packages/scipy/io/matlab/mio.py", line 217, in loadmat
    MR, _ = mat_reader_factory(f, **kwargs)
  File "/data/user1/pkgs/conda/envs/drc/lib/python3.7/site-packages/scipy/io/matlab/mio.py", line 72, in mat_reader_factory
    mjv, mnv = get_matfile_version(byte_stream)
  File "/data/user1/pkgs/conda/envs/drc/lib/python3.7/site-packages/scipy/io/matlab/miobase.py", line 235, in get_matfile_version
    maj_ind = int(tst_str[2] == b'I'[0])
IndexError: index out of range

换一种方式打开.mat文件,还是报错:

代码:

x = h5py.File(mat_file, 'w') data = x['data']

报错:

Traceback (most recent call last):
  File "/home/user1/test.py", line 90, in
    show_mat_flow(mat)
  File "/home/user1/test.py", line 60, in show_mat_flow
    data = x['data']
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "/home/user1/.local/lib/python3.6/site-packages/h5py/_hl/group.py", line 264, in __getitem__
    oid = h5o.open(self.id, self._e(name), lapl=self._lapl)
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "h5py/h5o.pyx", line 190, in h5py.h5o.open
KeyError: "Unable to open object (object 'data' doesn't exist)"


 

原因: 

暂时不是很确定原因。可能是mat文件保存时出了问题。建议numpy数组保存为mat文件时应使用 scipy.io.savemat,打开时应使用io.loadmat。这样一般不会出错。不建议和h5py.File交叉使用。

解决(仅供参考):

保存mat文件之后用io.loadmat去检查是否保存正确。

重新运行生成mat文件的程序。以上报错的文件在matlab中打开时也会报错。

qxqxqzz 原创文章 214获赞 86访问量 33万+ 关注 私信 展开阅读全文
作者:qxqxqzz



unknown version open mat type TO file object

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章