官网链接:

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

https://ww2.mathworks.cn/help/matlab/matlab_external/install-the-matlab-engine-for-python.html

安装用于 Python 的 MATLAB 引擎 API

要在 Python® 会话内启动 MATLAB® 引擎,必须先安装 Python 包形式的引擎 API。MATLAB 提供了标准的 Python setup.py 文件,用于通过 distutils 模块编译和安装引擎。您可以使用相同的 setup.py 命令在 Windows®、Mac 或 Linux® 系统上编译和安装引擎。

在安装之前,确认您的 Python 和 MATLAB 配置。

  • 您的系统具有受支持的 Python 版本和 MATLAB R2014b 或更新版本。要检查您的系统上是否已安装 Python,请在操作系统提示符下运行 Python。

  • 将包含 Python 解释器的文件夹添加到您的路径(如果尚未在该路径中)。

  • 找到 MATLAB 文件夹的路径。启动 MATLAB,并在命令行窗口中键入 matlabroot。复制 matlabroot 所返回的路径。

要安装引擎 API,请在操作系统提示符下执行以下命令,其中 matlabroot 是 MATLAB 文件夹的路径。您可能需要管理员权限才能执行这些命令。或者,使用在非默认位置安装用于 Python 的 MATLAB 引擎 API 中所述的非默认选项之一。

  • 在 Windows 系统中 -

    cd "matlabroot\extern\engines\python"
    python setup.py install
  • 在 Mac 或 Linux 系统中 -

    cd "matlabroot/extern/engines/python"
    python setup.py install

从 MATLAB 函数返回输出参数

您可以直接调用任何 MATLAB® 函数并将结果返回到 Python®。例如,要确定某个数是否为质数,请使用该引擎调用 isprime 函数。

import matlab.engine
eng = matlab.engine.start_matlab()
tf = eng.isprime(37)
print(tf)
True

从 MATLAB 函数返回多个输出参数

当使用引擎调用函数时,默认情况下该引擎会返回单个输出参数。如果您知道函数可能返回多个参数,请使用 nargout 参数指定输出参数的数量。

要确定两个数的最大公分母,请使用 gcd 函数。设置 nargout 以从 gcd 返回三个输出参数。

import matlab.engine
eng = matlab.engine.start_matlab()
t = eng.gcd(100.0,80.0,nargout=3)
print(t)
(20.0, 1.0, -1.0)

不从 MATLAB 函数返回任何输出参数

有些 MATLAB 函数不会返回任何输出参数。如果函数不返回任何参数,则将 nargout 设为 0。

通过 Python 打开 MATLAB 帮助浏览器。

import matlab.engine
eng = matlab.engine.start_matlab()
eng.doc(nargout=0)

MATLAB doc 函数将打开浏览器,但不会返回输出参数。如果您没有指定 nargout=0,引擎将报告错误。

停止执行函数

要停止执行 MATLAB 函数,请按 Ctrl+C。控制权将返回给 Python。

Call MATLAB Functions Asynchronously from Python

This example shows how to call the MATLAB® sqrt function asynchronously from Python® and retrieve the square root later.

The engine calls MATLAB functions synchronously by default. Control returns to Python only when the MATLAB function finishes. But the engine also can call functions asynchronously. Control immediately returns to Python while MATLAB is still executing the function. The engine stores the result in a Python variable that can be inspected after the function finishes.

Use the async argument to call a MATLAB function asynchronously.

import matlab.engine
eng = matlab.engine.start_matlab()
future = eng.sqrt(4.0,async=True)
ret = future.result()
print(ret)
2.0

Use the done method to check if an asynchronous call finished.

tf = future.done()
print(tf)
True

To stop execution of the function before it finishes, call future.cancel().

python调用matlab的更多相关文章

  1. Linux解决Python调用Matlab函数无法导入matlab.engine问题及其他注意事项

    问题描述 Linux系统,根据matlab官方文档说明,利用Matlab中的API来实现Python调用Matlab函数.具体方法见文档: https://ww2.mathworks.cn/help/ ...

  2. [python][matlab]使用python调用matlab程序

    问题引入 在做实验的时候,需要用到python和matlab工具来进行不同的处理,比如在run神经网络的时候,需要使用pytorch框架得到网络的各个参数,在得到参数后需要使用matlab进行聚类规划 ...

  3. Python的扩展接口[3] -> Matlab引擎 -> 使用 Python 调用 Matlab 程序

    Python - Matlab 目录 Python-Matlab 引擎 Python-Matlab 数组 Python-Matlab 基本操作 Python-Matlab 调用 m 文件 Matlab ...

  4. Windows中使用 Python 调用 Matlab 程序

    https://ww2.mathworks.cn/help/matlab/matlab_external/system-and-configuration-requirements.html http ...

  5. python调用matlab脚本

    在MATLAB和Python之间建个接口,从Python中调用MATLAB脚本或者是MATLAB的函数.内容不是很难,毕竟现成的接口已经有了,在这儿记录一下API使用的一些事项. 注:本篇使用的是MA ...

  6. Python调用Matlab2014b引擎

    用惯Python的你,是不是早已无法忍受matplotlib那丑陋无比的图以及蛋疼无比部署依赖? 当当当当,Matlab2014b的Python Engine API现已加入豪华午餐. 上次写了一篇文 ...

  7. 如何在Python中调用Matlab

    检查您的系统是否具有受支持的 Python 版本和 MATLAB R2014b 或更新版本.要检查您的系统上是否已安装 Python,请在操作系统提示符下运行 Python. 1)打开Prompt,输 ...

  8. [Python-MATLAB] 在Python中调用MATLAB的API

    可以参考官方的说明文档: http://cn.mathworks.com/help/matlab/matlab_external/get-started-with-matlab-engine-for- ...

  9. [转]numpy线性代数基础 - Python和MATLAB矩阵处理的不同

    转自:http://blog.csdn.net/pipisorry/article/details/45563695 http://blog.csdn.net/pipisorry/article/de ...

随机推荐

  1. Python标准库笔记(6) — struct模块

    该模块作用是完成Python数值和C语言结构体的Python字符串形式间的转换.这可以用于处理存储在文件中或从网络连接中存储的二进制数据,以及其他数据源. 用途: 在Python基本数据类型和二进制数 ...

  2. jQuery-对标签的样式操作

    一.操作样式类 // 1.给标签添加样式类 $("选择器").addClass("类名") // 2.移除标签的样式类 $("选择器").r ...

  3. 【Android开发】之MediaPlayer的错误分析

    最近在做媒体播放器,使用了Android自带的MediaPlayer,经常性会碰到MediaPlayer报错的情况,找过网上的,感觉总结的不是很好或者比较散.下面,我来总结一下使用MediaPlaye ...

  4. log4j与commons-logging slf4j的关系

    1. slf4j     他只提供一个核心slf4j api(就是slf4j-api.jar包),这个包只有日志的接口并没有实现     所以如果要使用就得再给它提供一个实现了些接口的日志包,     ...

  5. 安装node版本管理工具之NVM

    nvm是个啥?nvm是一个可以让你在同一台机器上安装和切换不同版本node的工具. 你可能会问,为什么会有这个工具?有时候在开发的时候,对node版本有强制要求,有的要求用最新版本,有的要求用稳定版本 ...

  6. Maven 基础知识

    Maven MavenMaven 简介 Maven MavenMaven 是 Apache Apache Apache 软件基金会组织维护的 软件基金会组织维护的 软件基金会组织维护的 软件基金会组织 ...

  7. opencv(5)GUI

    OpenCV的图形用户界面(Graphical User Interface, GUI)和绘图等相关功能也是很有用的功能,无论是可视化,图像调试还是我们这节要实现的标注任务,都可以有所帮助. 窗口循环 ...

  8. Centos之帮助命令

    帮助命令man  (manual) 比如我们可以看下man命令的解释 [root@localhost ~]# man man MAN(1)                               ...

  9. 【剑指Offer面试题】 九度OJ1389:变态跳楼梯

    转自:http://www.myexception.cn/program/1973966.html 时间限制:1 秒内存限制:32 兆特殊判题:否提交:2331解决:1332 题目描述: 一只青蛙一次 ...

  10. python 常用的标准库及第三方库

    标准库Python拥有一个强大的标准库.Python语言的核心只包含数字.字符串.列表.字典.文件等常见类型和函数,而由Python标准库提供了系统管理.网络通信.文本处理.数据库接口.图形系统.XM ...