最近使用一组mat格式的数据,在读取到一半类别数据时报错。一开始以为是数据损坏,经MATLAB查看,排除了该问题。后来分析和查错发现,是由于数据本身采用了不同版本的MATLAB保存,导致读取的时候出了一些问题。现总结两种不同的mat文件读取方式与错误读取时的报错,与大家分享,不足之处欢迎指正。
1. MATLAB v7.3 的mat文件使用h5py读取
示例:
import h5py
f = h5py.File('a.mat')
# 查看mat文件中的所有keys
list(f.keys())
# 此处‘a’对应的value为三维,print后显示所有value; 注意,如果是data f['a'],print后显示文件类型和模式
data = f['a'][:,:,:]
print(data)
若该类型的mat文件使用sicpy.io.loadmat读取,将会报错类似如下形式:
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.5/dist-packages/scipy/io/matlab/mio.py", line 207, in loadmat
MR, file_opened = mat_reader_factory(file_name, appendmat, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/scipy/io/matlab/mio.py", line 69, in mat_reader_factory
raise NotImplementedError('Please use HDF reader for matlab v7.3 files')
NotImplementedError: Please use HDF reader for matlab v7.3 files
2. 其他版本mat文件可尝试使用scipy.io.loadmat
示例:
import scipy.io as scio
f = scio.loadmat('/home/dell/Way.mat') # data同样为字典格式
print(f['C16']) #打印key为C16的value
若该类型用h5py读取,可能报错如下:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/h5py/_hl/files.py", line 190, in make_fid
fid = h5f.open(name, h5f.ACC_RDWR, fapl=fapl)
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/h5f.pyx", line 85, in h5py.h5f.open
OSError: Unable to open file (file signature not found)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/h5py/_hl/files.py", line 193, in make_fid
fid = h5f.open(name, h5f.ACC_RDONLY, fapl=fapl)
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/h5f.pyx", line 85, in h5py.h5f.open
OSError: Unable to open file (file signature not found)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.5/dist-packages/h5py/_hl/files.py", line 394, in __init__
swmr=swmr)
File "/usr/local/lib/python3.5/dist-packages/h5py/_hl/files.py", line 195, in make_fid
fid = h5f.create(name, h5f.ACC_EXCL, fapl=fapl, fcpl=fcpl)
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/h5f.pyx", line 105, in h5py.h5f.create
OSError: Unable to create file (unable to open file: name = '/home/dell/zhou/GAN-MSTAR/land_data/C17_HighWay.mat', errno = 17, error message = 'File exists', flags = 15, o_flags = c2)
3. 总结
使用数据前,先确定数据的类型、格式,避免走弯路。
参考资料:
https://github.com/h5py/h5py/issues/757
https://docs.scipy.org/doc/scipy-0.19.0/reference/generated/scipy.io.loadmat.html