一、mat文件
mat數據格式是Matlab的數據存儲的標准格式。在Matlab中主要使用load()函數導入一個mat文件,使用save()函數保存一個mat文件。對於文件
load('data.mat') save('data_1.mat','A')
其中,'A'表示要保存的內容。
二、python中讀取mat文件
在python中可以使用scipy.io中的函數loadmat()讀取mat文件,函數savemat保存文件。
1、讀取文件
如上例:
#coding:UTF-8 import scipy.io as scio dataFile = 'E://data.mat' data = scio.loadmat(dataFile)
注意,讀取出來的data是字典格式,可以通過函數type(data)查看。
print type(data)
結果顯示
<type 'dict'>
找到mat文件中的矩陣:
print data['A']
結果顯示
[[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 。。。。。。。。。。。 0. 0. 0. 0. 0. 0. 0. 0.36470588 0.90196078 0.99215686 0.99607843 0.99215686 0.99215686 0.78431373 0.0627451 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 。。。。。。。。。。。。 0.94117647 0.22745098 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.30196078 。。。。。。。 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]]
格式為:
<type 'numpy.ndarray'>
即為numpy中的矩陣格式。
2、保存文件
將這里的data['A']矩陣重新保存到一個新的文件dataNew.mat中:
dataNew = 'E://dataNew.mat' scio.savemat(dataNew, {'A':data['A']})
注意:是以字典的形式保存