是否可以在Python中读取二进制MATLAB .mat文件?
我看到SciPy声称支持读取.mat文件,但我没有成功。我安装了SciPy 0.7.0版本,但找不到loadmat()方法。
是否可以在Python中读取二进制MATLAB .mat文件?
我看到SciPy声称支持读取.mat文件,但我没有成功。我安装了SciPy 0.7.0版本,但找不到loadmat()方法。
当前回答
有一个用于此任务的很棒的库:pymatreader。
按照下面的步骤做:
安装包:pip Install pymatreader 导入此包的相关函数:from pymatreader Import read_mat 使用函数读取matlab结构:data = read_mat('matlab_struct.mat') 使用data.keys()来定位数据实际存储的位置。
钥匙通常会像:dict_keys([‘__header__’,‘__version__’,‘__globals__’,' data_opp '])。其中data_opp将是存储数据的实际键。当然,这个键的名称可以在不同的文件之间更改。
最后一步-创建你的数据帧:my_df = pd.DataFrame(data['data_opp'])
就是这样:)
其他回答
Scipy可以很好地加载.mat文件。 我们可以使用get()函数将其转换为numpy数组。
mat = scipy.io.loadmat('point05m_matrix.mat')
x = mat.get("matrix")
print(type(x))
print(len(x))
plt.imshow(x, extent=[0,60,0,55], aspect='auto')
plt.show()
需要导入,导入scipy.io…
import scipy.io
mat = scipy.io.loadmat('file.mat')
安装了MATLAB 2014b或更新版本后,可以使用MATLAB for Python引擎:
import matlab.engine
eng = matlab.engine.start_matlab()
content = eng.load("example.mat", nargout=1)
将mat文件读入混合数据类型的pandas dataFrame
import scipy.io as sio
mat=sio.loadmat('file.mat')# load mat-file
mdata = mat['myVar'] # variable in mat file
ndata = {n: mdata[n][0,0] for n in mdata.dtype.names}
Columns = [n for n, v in ndata.items() if v.size == 1]
d=dict((c, ndata[c][0]) for c in Columns)
df=pd.DataFrame.from_dict(d)
display(df)
读取文件
import scipy.io
mat = scipy.io.loadmat(file_name)
检查MAT变量的类型
print(type(mat))
#OUTPUT - <class 'dict'>
字典中的键是MATLAB变量,值是分配给这些变量的对象。