上次写了一篇文章,讲用C++调用Matlab的绘图引擎。
不过呢有句话怎么说来着?人生苦短,我用Python。
这次,就说一说怎么用Python调用Matlab的引擎。果然Python大法好,这个可比C++要容易太多了。
当然注意,Matlab2014b的Engine才开始提供对Python的API支持,先前版本是没有的。
过程非常简单,第一步是安装,假设Matlabroot是Matlab的安装根目录
找到你的Matlab安装根目录,然后Shell进入matlabroot\extern\engines\python目录中,执行
python setup.py install
就完事了。注意,一定要用管理员权限执行。
不用管理员权限的安装方法稍微复杂一点点:
cd "matlabroot\extern\engines\python"
python setup.py build --build-base builddir install --install-base installdir
将installdir添加到Python的包搜索路径中,再加入到PYTHONPATH环境变量中即可。
Matlab的Python引擎怎么用呢? 更简单了:
import matlab.engine 就可以开始了。
###Matlab Engine for Python
#Call Matlab Function from Python
------------------------------
##Step 1: Installation
#Install with Administrator Privileges
cd "matlabroot\extern\engines\python"
python setup.py install
#Install without Administrator Privileges
cd "matlabroot\extern\engines\python"
python setup.py build --build-base builddir install --install-base installdir
Include 'installdir' in the search path for Python packages
Add 'installdir' to the PYTHONPATH environment variavle
------------------------------
##Step 2: Using Matlab Engine
#Start and quit
import matlab.engine
eng = matlab.engine.start_matlab()
eng.quit()
#Call Matlab Functions:
#Just call with form eng.xxx()
#the function xxx should in the namespace of matlab.
#Asynchronously Call
import matlab.engine
eng = matlab.engine.start_matlab()
future = eng.sqrt(4.0,async=True)
ret = future.result()
print(ret)
#WorkSpace Usage:
import matlab.engine
eng = matlab.engine.start_matlab()
eng.workspace['y'] = x
a = eng.eval('sqrt(y)')
print(a)
#Skills for unsupported features in python
#eng.eval()
import matlab.engine
eng = matlab.engine.start_matlab()
eng.eval("T = readtable('patients.dat');",nargout=0)
#Plot With Matlab:
import matlab.engine
eng = matlab.engine.start_matlab()
data = eng.peaks(100)
eng.mesh(data)
------------------------------
然后是一段测试Python代码:
importmatlabimportmatlab.engineimporttimedefbasic_test(eng):print "Basic Testing Begin"
print "eng.power(100,2) = %d"%eng.power(100,2)print "eng.max(100,200) = %d"%eng.max(100,200)print "eng.rand(5,5) ="
print eng.rand(5,5)print "eng.randi(matlab.double([1,100]),matlab.double([3,4]))"%\
eng.randi(matlab.double([1,100]),matlab.double([3,4]))print "Basic Testing Begin"
defplot_test(eng):print "Plot Testing Begin"eng.workspace['data'] =\
eng.randi(matlab.double([1,100]),matlab.double([30,2]))
eng.eval("plot(data(:,1),'ro-')")
eng.hold('on',nargout=0)
eng.eval("plot(data(:,2),'bx--')")print "Plot testing end"
defaudio_test(eng,freq,length):print "Audio Testing Begin"eval_str= "f = %d;t=%d;"%(freq,length)
eng.eval(eval_str,nargout=0)
eng.eval('fs = 44100;T=1/fs;t=(0:T:t);',nargout =0)
eng.eval('y = sin(2 * pi * f * t);',nargout =0)
eng.eval('sound(y,fs);',nargout =0)
time.sleep(length)print "Audio Testing End"
deffourier_test(eng):pass
defdemo(eng):
basic_test(eng)
plot_test(eng)
audio_test(eng,680,1)if __name__ == "__main__":print "Initializing Matlab Engine"eng=matlab.engine.start_matlab()print "Initializing Complete!"demo(eng)print "Exiting Matlab Engine"
print "Press Any Key to Exit"raw_input();
eng.quit()print "Bye-Bye"eng=matlab.engine.start_matlab()
eng.quit()
可以发现,Python调用Matlab的方式更为科学,通过engine.workspace以字典的形式存取变量。通过反射的方式,直接以eng.func()的形式调用Matlab原生函数。
用来可视化数据简直不要太方便。