Python MATLAB

本文介绍如何在Python环境中通过安装matlab.engine来调用MATLAB的功能,包括执行MATLAB脚本文件及方法的具体步骤,并给出实例演示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值