通过 Python 调用 MATLAB 函数

MATLB安装于/opt/local/MATLAB/R2017a目录

一、Python安装MATLAB引擎

为了不“污染”MATLAB,先建立engines/python的备份(两个分别用于python2,python3)

sudo a+rw /opt/local/MATLAB/R2017a/extern/engines
cp -Rf /opt/local/MATLAB/R2017a/extern/engines/python /opt/local/MATLAB/R2017a/extern/engines/python-27
cp -Rf /opt/local/MATLAB/R2017a/extern/engines/python /opt/local/MATLAB/R2017a/extern/engines/python-35

安装:

cd /opt/local/MATLAB/R2017a/extern/engines/python-27
python setup.py build 
sudo python setup.py install

这里安装的是Python2,安装python3如下:

cd /opt/local/MATLAB/R2017a/extern/engines/python-35
python3 setup.py build 
sudo python3 setup.py install

二、验证:

pip list一下发现有下面这个模块了:

matlabengineforpython         R2017a 

三、使用(调用MATLAB内置函数isprime)

~$ python
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matlab.engine
>>> eng = matlab.engine.start_matlab()
>>> tf = eng.isprime(37)
>>> print(tf)
True
>>> 

猜你喜欢

转载自my.oschina.net/u/2245781/blog/1823503