Python读取、保存、查看.mat文件

时间:2022-04-24 07:07:10

使用Python读取、保存、查看.mat文件,这样可以结合matlab和python来用。

一个Python例子如下:

import scipy.io as sio  
import matplotlib.pyplot as plt
import numpy as np

# 创建4个变量,并赋予相应的取值
sio.savemat('testpython.mat', {'a': 1,'b': 2,'c': 3,'d': 4})

# 创建了一个变量x,并赋予一个矩阵
sio.savemat('testpython2.mat', {'x': [[1, 2, 3, 4],[ 5, 6, 7, 8]]})

data = sio.loadmat('testpython.mat')
x1 = data['a']
x2 = data['b']
x3 = data['c']
x4 = data['d']

sio.whosmat('testpython.mat')


很奇怪的是,在我的笔记本电脑上,调用loadmat和whosmat函数时Python会崩溃,但savemat没问题。在我的台式机上,这三个函数都可以正常调用没有问题。


我的笔记本上Python安装的是Anaconda,64位的Python,相关版本信息如下:

Python 2.7.11 |Anaconda 2.5.0 (64-bit)| (default, Jan 29 2016, 14:26:21) [MSC v.
1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>>


我的台式机上Python安装的是Python(x,y),32位的Python。