从 Python 调用 MATLAB 的模块
项目描述
适用于 Python 的 MATLAB 引擎 API
适用于 Python® 的 MATLAB® Engine API 提供了一个包,可将 MATLAB 功能直接与 Python 应用程序集成,创建一个接口以从 Python 代码调用 MATLAB 安装中的函数。
要求
所需的 MathWorks 产品
- MATLAB 发布 R2022b
所需的第三方产品
- Python 3.8、3.9 或 3.10
- 可以在此处找到 MATLAB 版本支持的 Python 版本。
安装
视窗
MATLAB Engine API for Python 可以直接从 Python Package Index 安装。
$ python -m pip install matlabengine==9.13.1
Linux®
在安装之前,通过调用matlabroot
MATLAB 命令行窗口来检查 MATLAB 的默认安装位置。默认情况下,Linux 将 MATLAB 安装在:
/usr/local/MATLAB/R2022b
当 MATLAB 未安装在默认位置时,必须将 MATLAB 根目录中的 bin/ architecture目录添加到环境变量中。该路径可以添加到 shell 启动配置文件中的环境变量(例如,.bashrc 用于 bash shell 或 .tcshrc 用于 tcsh)。
# in .bashrc
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<matlabroot>/bin/glnxa64
# in .tcshrc
setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:<matlabroot>/bin/glnxa64
MATLAB Engine API for Python 可以直接从 Python Package Index 安装。
$ python -m pip install matlabengine==9.13.1
苹果系统
在安装之前,通过调用matlabroot
MATLAB 命令行窗口来检查 MATLAB 的默认安装位置。默认情况下,macOS 将 MATLAB 安装在:
/Applications/MATLAB_R2022b.app
当 MATLAB 未安装在默认位置时,必须将 MATLAB 根目录中的 bin/ architecture目录添加到环境变量中。该路径可以添加到 shell 启动配置文件中的环境变量(例如,.bashrc 用于 bash shell 或 .tcshrc 用于 tcsh)。
# in .bashrc
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:<matlabroot>/bin/maci64
# in .tcshrc
setenv DYLD_LIBRARY_PATH ${DYLD_LIBRARY_PATH}:<matlabroot>/bin/maci64
MATLAB Engine API for Python 可以直接从 Python Package Index 安装。
$ python -m pip install matlabengine==9.13.1
入门
- 启动 Python。
- 将
matlab.engine
包导入 Python 会话。 - 通过调用启动一个新的 MATLAB 进程
start_matlab
。该start_matlab
函数返回一个 Python 对象,使您能够传递数据并调用 MATLAB 执行的函数。
>>> import matlab.engine
>>> eng = matlab.engine.start_matlab()
>>> eng.sqrt(4.0)
2.0
- 调用
exit
orquit
函数来停止引擎。在引擎运行的情况下退出 Python 会停止引擎及其 MATLAB 进程。
>>> eng.quit()
有关高级启动示例,请参阅启动和停止 MATLAB Engine for Python。
例子
您可以直接调用任何 MATLAB 函数并将结果返回给 Python。
>>> eng.plus(2, 3)
5
>>> eng.isprime(37)
True
>>> eng.gcd(100.0, 80.0, nargout=3)
(20.0, 1.0, -1.0)
有关更多使用示例,请参阅从 Python 调用 MATLAB 函数。
限制
可以在此处找到适用于 Python 的 MATLAB 引擎 API 的限制。
故障排除
请参阅排除 Python 中的 MATLAB 错误以获取故障排除帮助。
执照
该软件根据 MathWorks XSLA 许可证获得许可,该许可证可在此存储库中的 LICENSE.txt 文件中找到。
支持
可以在此处提交技术问题或增强请求。
版权所有 © 2022 MathWorks, Inc.。保留所有权利。
Linux® 是 Linus Torvalds 在美国和其他国家的注册商标。
Mac OS 是 Apple Inc. 在美国和其他国家/地区注册的商标。
“Python”和 Python 徽标是 Python 软件基金会的商标或注册商标,MathWorks 在基金会许可下使用。
项目详情
matlabengine -9.13.1.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | b844ef46a2f5309deb9df0727fe089666a3340ed9a8cb57b3041f7da00720b16 |
|
MD5 | e013a0d21da258e12ba32a688f272114 |
|
布莱克2-256 | c5a32ed195d210eb61b80bcd98c2bedb69abbb2aebb8ee1b6d1a1484e9864abc |