对于 MATLAB v7.3,您可以使用hdf5storage
这需要h5py
,在此处下载文件,解压,然后键入:python setup.py install
从命令提示符。https://pypi.python.org/pypi/hdf5storage https://pypi.python.org/pypi/hdf5storage
import h5py
import hdf5storage
import numpy as np
matfiledata = {} # make a dictionary to store the MAT data in
matfiledata[u'variable1'] = np.zeros(100) # *** u prefix for variable name = unicode format, no issues thru Python 3.5; advise keeping u prefix indicator format based on feedback despite docs ***
matfiledata[u'variable2'] = np.ones(300)
hdf5storage.write(matfiledata, '.', 'example.mat', matlab_compatible=True)
如果 MATLAB 无法一次加载整个内容,我认为您必须将其保存在不同的变量中matfiledata[u'chunk1'] matfiledata[u'chunk2'] matfiledata[u'chunk3']
etc.
然后在 MATLAB 中,如果将每个块保存为变量
load(filename,'chunk1')
do stuff...
clear chunk1
load(filename,'chunk2')
do stuff...
clear chunk2
etc.
hdf5storage.savemat 有一个参数,允许将来将文件正确读入 Python,因此值得检查,并遵循 scipy.io.loadmat 格式...尽管如果将数据从 MATLAB 保存到使导入回 Python 变得容易:
MATLAB
save('example.mat','-v7.3')
Python
matdata = hdf5storage.loadmat('example.mat')
它将作为字典加载回 Python,然后您可以将其转换为您需要的任何数据类型。