MATLB安裝於/opt/local/MATLAB/R2017a目錄python
1、Python安裝MATLAB引擎linux
爲了避免「污染」MATLAB,先創建engines/python的備份(兩個分別用於python2,python3)c++
sudo chmod 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 sudo chmod 755 /opt/local/MATLAB/R2017a/extern/engines
安裝:python2.7
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
2、驗證:ui
pip list一下發現有下面這個模塊了:code
matlabengineforpython R2017a
3、使用(調用MATLAB內置函數isprime)orm
~$ 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 >>>
4、MATLAB中調用python函數ip
MATLAB R2015a 開始支持直接調用 Python:string
>> pyversion version: '2.7' executable: '/usr/bin/python' library: 'libpython2.7.so.1.0' home: '/usr' isloaded: 0 >> py.print('Hello world!') Hello world! >> py.sum([0,1,2]) ans = 3 >> py.print(py.pow(int32(2), int32(1000))) 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376 >>
導入.py文件(用edit命令建立test1.py):
>> edit test1.py >> py.test1.c1(4,7) ans = 11 >>
其中test1.py的內容以下:
def c1(a1,b1): c=a1+b1 return c
但有時import有些模塊會出現libstdc++.so.6版本不對的狀況,如py.importlib.import_module('cv2')
/opt/local/MATLAB/R2017a/bin/glnxa64/../../sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found
依賴GLIBCXX_3.4.21,但Matlab自帶的那個最高支持到GLIBCXX_3.4.20
$ strings /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25|grep GLIBC GLIBCXX_3.4 GLIBCXX_3.4.1 GLIBCXX_3.4.2 GLIBCXX_3.4.3 GLIBCXX_3.4.4 GLIBCXX_3.4.5 GLIBCXX_3.4.6 GLIBCXX_3.4.7 GLIBCXX_3.4.8 GLIBCXX_3.4.9 GLIBCXX_3.4.10 GLIBCXX_3.4.11 GLIBCXX_3.4.12 GLIBCXX_3.4.13 GLIBCXX_3.4.14 GLIBCXX_3.4.15 GLIBCXX_3.4.16 GLIBCXX_3.4.17 GLIBCXX_3.4.18 GLIBCXX_3.4.19 GLIBCXX_3.4.20 GLIBCXX_3.4.21 GLIBCXX_3.4.22 GLIBCXX_3.4.23 GLIBCXX_3.4.24 GLIBCXX_3.4.25 GLIBC_2.2.5 GLIBC_2.3 GLIBC_2.14 GLIBC_2.4 GLIBC_2.18 GLIBC_2.16 GLIBC_2.3.4 GLIBC_2.17 GLIBC_2.3.2 GLIBCXX_DEBUG_MESSAGE_LENGTH $ strings /opt/local/MATLAB/R2017a/sys/os/glnxa64/libstdc++.so.6.0.20|grep GLIBC GLIBCXX_3.4 GLIBCXX_3.4.1 GLIBCXX_3.4.2 GLIBCXX_3.4.3 GLIBCXX_3.4.4 GLIBCXX_3.4.5 GLIBCXX_3.4.6 GLIBCXX_3.4.7 GLIBCXX_3.4.8 GLIBCXX_3.4.9 GLIBCXX_3.4.10 GLIBCXX_3.4.11 GLIBCXX_3.4.12 GLIBCXX_3.4.13 GLIBCXX_3.4.14 GLIBCXX_3.4.15 GLIBCXX_3.4.16 GLIBCXX_3.4.17 GLIBCXX_3.4.18 GLIBCXX_3.4.19 GLIBCXX_3.4.20 GLIBC_2.3 GLIBC_2.2.5 GLIBC_2.3.2 .........
解決方法把/opt/local/MATLAB/R2017a/sys/os/glnxa64/libstdc++.so.6連接改過來(比較粗暴的作法,暫時沒發現matlab運行有問題)
sudo rm /opt/local/MATLAB/R2017a/sys/os/glnxa64/libstdc++.so.6 sudo ln -s /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.25 /opt/local/MATLAB/R2017a/sys/os/glnxa64/libstdc++.so.6 不行再改回來 sudo rm /opt/local/MATLAB/R2017a/sys/os/glnxa64/libstdc++.so.6 sudo ln -s /opt/local/MATLAB/R2017a/sys/os/glnxa64/libstdc++.so.6.0.20 /opt/local/MATLAB/R2017a/sys/os/glnxa64/libstdc++.so.6