无法用scipy读取MAT文件

时间:2017-03-05 10:35:39

标签: python matlab scipy

我正在尝试使用scipy

读取matlab文件
import scipy.io as sio

data = sio.loadmat(filepath)

但我收到了错误

ValueError: Did not fully consume compressed contents of an miCOMPRESSED element. This can indicate that the .mat file is corrupted.

在Matlab中我可以毫无问题地打开这个文件。我还试图再次保存它,但没有改变...... 你能救我吗?

此处:https://drive.google.com/drive/folders/0B3vXKJ_zYaCJanZfOUVIcGJyR0E 你可以找到以同样方式保存的2个文件..

我可以打开part_000,但不能打开part_001 ....为什么? :(

4 个答案:

答案 0 :(得分:4)

这个问题似乎是由压缩造成的。 {7}文件将从版本7开始自动压缩。

因此,我建议尝试将文件保存在较早的未压缩.mat文件版本6中:

.mat

答案 1 :(得分:1)

我可以使用Octave加载这两个文件,并重写导致问题的文件

>> data1 = load('part_0001.mat');
>> save -v7 part_0002.mat -struct data1

在Python中,重写的文件加载正常,就像您的0000.mat文件一样。

In [8]: data2=loadmat('part_0002.mat')
In [10]: data2.keys()
Out[10]: dict_keys(['RealTime', 'AccNorm', 'Alt', 'FsP', 'DeviceTime', 'FsA', 'Acc', 'imatemp', 'Time', '__version__', '__globals__', '__header__'])

重写的文件实际上要小一些。 V6文件为13M,也可以加载。

>> save -v6 part_0003.mat -struct data1

因此loadmat's处理V7格式时必定存在一些问题。

答案 2 :(得分:0)

有时mat文件损坏,因此Matlab无法识别数据类型并且无法加载它。因此在保存Mat文件时。尝试通过设置long_field_names = True

保存Mat文件
  

scipy.io.savemat(filename,long_field_names = True)

答案 3 :(得分:0)

问题出在scipy.io.loadmat的{​​{1}}关键字参数上,默认为verify_compressed_data_integrity。它试图对标头进行一些错误检查,但即使提取的数据很好,也可能引发错误。参见this related GitHub issue。我不确定全职关闭此功能的含义,但是如果您使用以下功能,它可以同时解决您的问题(我无法根据您的数据进行检查,它在提供的URL上不再可用)

True
相关问题