官网链接:

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. CCN与CDN区别

    CCN与CDN区别 相同点: 1.针对目前互联网上存在问题,提出解决方案,让数据传输更快更稳定. 2.都均衡网络流量. 区别: 1.CDN是内容分发网络,是基于目前的TCP/IP体系结构的补充方法.C ...

  2. 2016.07.15——istringstream测试

    istringstream测试 1.istringstream strcin(str),字符串(str)可以包括多个单词,单词之间使用空格分开 #include "stdafx.h" ...

  3. 【codeforces】【比赛题解】#937 CF Round #467 (Div. 2)

    没有参加,但是之后几天打了哦,第三场AK的CF比赛. CF大扫荡计划正在稳步进行. [A]Olympiad 题意: 给\(n\)个人颁奖,要满足: 至少有一个人拿奖. 如果得分为\(x\)的有奖,那么 ...

  4. 【工具记录】Linux口令破解

    1.基础知识 /etc/passwd:记录着用户的基本属性,所有用户可读 字段含义如下: 用户名:口令:用户标识号:组标识号:注释性描述:主目录:登录Shell eg: root:x:0:0:root ...

  5. jQuery学习(二) 自定义扩展函数

    jQuery函数调用写法很优雅,在项目开发过程中,有需要自定义函数经常被使用到,将这些函数放置到项目ExtTool.js中,为了编码方式的统一,也希望这些自定义函数与jQuery函数一致的调用方式.在 ...

  6. Jenkins忘记用户名密码

    一.进入C盘.jenkins配置文件中找到config.xml 需要删除一下内容: <useSecurity>true</useSecurity> <authorizat ...

  7. Oracle学习笔记:decode函数

    decode函数主要作用:将查询结果翻译成其他值(即以其他形式变现出来) 使用方法: SELECT DECODE(colunm_name,值1,翻译值1,值2,翻译值2……值n,翻译值n,缺省值) F ...

  8. CVE-2011-0104 Microsoft Office Excel缓冲区溢出漏洞 分析

    漏洞简述   Microsoft Excel是Microsoft Office组件之一,是流行的电子表格处理软件.        Microsoft Excel中存在缓冲区溢出漏洞,远程攻击者可利用此 ...

  9. 组件库按需加载 借助babel-plugin-import实现

    前段时间一直在基于webpack进行前端资源包的瘦身.在项目中基于路由进行代码分离,http://www.cnblogs.com/legu/p/7251562.html.对于公司内部的组件库,所有内容 ...

  10. USACO 5.3 Milk Measuring

    Milk MeasuringHal Burch Farmer John must measure Q (1 <= Q <= 20,000) quarts of his finest mil ...