Python读写.mat文件
Python读写.mat文件
读写模块scipy.io
import scipy.io as sio
# 读取文件
mat = sio.loadmat(path)
#写入文件
sio.savemat(outputPath, mat)
python创建Matlab Cell
用python的字典和列表构建Matlab的Cell
scipy.io在写入时会将python的列表转换成numpy的array,所以在编辑的时候使用列表或者numpy的array都是可以的,不过列表数据结构在编辑的时候更方便。
>>> import scipy.io as sio
>>> toto = [{}, {}]
>>> toto[1]['Site']=['Tataouine']
>>> toto[0]['weapon'] = [{}, {}]
>>> toto[0]['weapon'][1]['Name']='fleurs'
>>> toto[0]['weapon'][0]['Name']='bonbons'
>>> toto
[{'weapon': [{'Name': 'bonbons'}, {'Name': 'fleurs'}]}, {'Site': ['Tataouine']}]
>>> sio.savemat('toto.mat', {'toto':toto}) #要以字典的形式写入
在Matlab中是这样的
>> load('toto')
>> toto
toto =
[1x1 struct] [1x1 struct]
>> toto{1}.weapon{2}.Name
ans =
fleurs