Python MATLAB

Python中要想調用MATLAB的功能,需要安裝matlab.engine,此工具在matlab的安裝目錄下,比如:

C:\Users\admin>cd C:\apps\MATLAB\R2019a\extern\engines\python 
C:\apps\MATLAB\R2019a\extern\engines\python>

可以通過執行setup.py腳本進行安裝:

C:\apps\MATLAB\R2019a\extern\engines\python>python setup.py install
running install
running build
running build_py
creating build
creating build\lib
creating build\lib\matlab
copying dist\matlab\mlarray.py -> build\lib\matlab
copying dist\matlab\mlexceptions.py -> build\lib\matlab
copying dist\matlab\__init__.py -> build\lib\matlab
creating build\lib\matlab\engine
copying dist\matlab\engine\basefuture.py -> build\lib\matlab\engine
copying dist\matlab\engine\engineerror.py -> build\lib\matlab\engine
copying dist\matlab\engine\enginehelper.py -> build\lib\matlab\engine
copying dist\matlab\engine\enginesession.py -> build\lib\matlab\engine
copying dist\matlab\engine\fevalfuture.py -> build\lib\matlab\engine
copying dist\matlab\engine\futureresult.py -> build\lib\matlab\engine
copying dist\matlab\engine\matlabengine.py -> build\lib\matlab\engine
copying dist\matlab\engine\matlabfuture.py -> build\lib\matlab\engine
copying dist\matlab\engine\__init__.py -> build\lib\matlab\engine
creating build\lib\matlab\_internal
copying dist\matlab\_internal\mlarray_sequence.py -> build\lib\matlab\_internal
copying dist\matlab\_internal\mlarray_utils.py -> build\lib\matlab\_internal
copying dist\matlab\_internal\__init__.py -> build\lib\matlab\_internal
running install_lib
creating C:\apps\Python\Python37\Lib\site-packages\matlab
creating C:\apps\Python\Python37\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\basefuture.py -> C:\apps\Python\Python37\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\engineerror.py -> C:\apps\Python\Python37\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\enginehelper.py -> C:\apps\Python\Python37\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\enginesession.py -> C:\apps\Python\Python37\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\fevalfuture.py -> C:\apps\Python\Python37\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\futureresult.py -> C:\apps\Python\Python37\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\matlabengine.py -> C:\apps\Python\Python37\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\matlabfuture.py -> C:\apps\Python\Python37\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\_arch.txt -> C:\apps\Python\Python37\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\__init__.py -> C:\apps\Python\Python37\Lib\site-packages\matlab\engine
copying build\lib\matlab\mlarray.py -> C:\apps\Python\Python37\Lib\site-packages\matlab
copying build\lib\matlab\mlexceptions.py -> C:\apps\Python\Python37\Lib\site-packages\matlab
creating C:\apps\Python\Python37\Lib\site-packages\matlab\_internal
copying build\lib\matlab\_internal\mlarray_sequence.py -> C:\apps\Python\Python37\Lib\site-packages\matlab\_internal
copying build\lib\matlab\_internal\mlarray_utils.py -> C:\apps\Python\Python37\Lib\site-packages\matlab\_internal
copying build\lib\matlab\_internal\__init__.py -> C:\apps\Python\Python37\Lib\site-packages\matlab\_internal
copying build\lib\matlab\__init__.py -> C:\apps\Python\Python37\Lib\site-packages\matlab
byte-compiling C:\apps\Python\Python37\Lib\site-packages\matlab\engine\basefuture.py to basefuture.cpython-37.pyc
byte-compiling C:\apps\Python\Python37\Lib\site-packages\matlab\engine\engineerror.py to engineerror.cpython-37.pyc
byte-compiling C:\apps\Python\Python37\Lib\site-packages\matlab\engine\enginehelper.py to enginehelper.cpython-37.pyc
byte-compiling C:\apps\Python\Python37\Lib\site-packages\matlab\engine\enginesession.py to enginesession.cpython-37.pyc
byte-compiling C:\apps\Python\Python37\Lib\site-packages\matlab\engine\fevalfuture.py to fevalfuture.cpython-37.pyc
byte-compiling C:\apps\Python\Python37\Lib\site-packages\matlab\engine\futureresult.py to futureresult.cpython-37.pyc
byte-compiling C:\apps\Python\Python37\Lib\site-packages\matlab\engine\matlabengine.py to matlabengine.cpython-37.pyc
byte-compiling C:\apps\Python\Python37\Lib\site-packages\matlab\engine\matlabfuture.py to matlabfuture.cpython-37.pyc
byte-compiling C:\apps\Python\Python37\Lib\site-packages\matlab\engine\__init__.py to __init__.cpython-37.pyc
byte-compiling C:\apps\Python\Python37\Lib\site-packages\matlab\mlarray.py to mlarray.cpython-37.pyc
byte-compiling C:\apps\Python\Python37\Lib\site-packages\matlab\mlexceptions.py to mlexceptions.cpython-37.pyc
byte-compiling C:\apps\Python\Python37\Lib\site-packages\matlab\_internal\mlarray_sequence.py to mlarray_sequence.cpython-37.pyc
byte-compiling C:\apps\Python\Python37\Lib\site-packages\matlab\_internal\mlarray_utils.py to mlarray_utils.cpython-37.pyc
byte-compiling C:\apps\Python\Python37\Lib\site-packages\matlab\_internal\__init__.py to __init__.cpython-37.pyc
byte-compiling C:\apps\Python\Python37\Lib\site-packages\matlab\__init__.py to __init__.cpython-37.pyc
running install_egg_info
Writing C:\apps\Python\Python37\Lib\site-packages\matlabengineforpython-R2018a-py3.7.egg-info

C:\apps\MATLAB\R2019a\extern\engines\python>
C:\apps\MATLAB\R2019a\extern\engines\python>
C:\apps\MATLAB\R2019a\extern\engines\python>

一、執行matlab腳本文件:

在Idea中的Python項目下新建文件夾,並建立learn.py和triarea.m:

triarea.m的文件內容如下:

b = 5;
h = 3;
a = 0.5*(b.* h)

然後就可以在learn.py中執行該腳本:

import matlab.engine

eng = matlab.engine.start_matlab()

eng.triarea(nargout=0)

輸出如下:

C:\apps\Python\Python37\python.exe -B D:/workspace/Idea/Python/python/main/learn/matlab/learn.py

a =

    7.5000


Process finished with exit code 0

二、執行matlab方法。

triarea中添加方法:

function a = triarea(b, h)
a = 0.5 * (b.* h);

learn.py內容如下:

import matlab.engine

eng = matlab.engine.start_matlab()

ret = eng.triarea(1.0, 5.0)
print(ret)

輸出如下:

C:\apps\Python\Python37\python.exe -B D:/workspace/Idea/Python/python/main/learn/matlab/learn.py
2.5

Process finished with exit code 0

具體含義可以參考官方資料:

https://ww2.mathworks.cn/help/matlab/matlab_external/call-user-script-and-function-from-python.html?lang=en

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章