zoukankan      html  css  js  c++  java
  • python调用matlab

    官网链接:

    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 "matlabrootexternenginespython"
      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().

  • 相关阅读:
    例题6-8 Tree Uva548
    例题6-7 Trees on the level ,Uva122
    caffe Mac 安装
    Codeforces Round #467 (Div. 1) B. Sleepy Game
    Educational Codeforces Round37 E
    Educational Codeforces Round 36 (Rated for Div. 2) E. Physical Education Lessons
    Good Bye 2017 E. New Year and Entity Enumeration
    Good Bye 2017 D. New Year and Arbitrary Arrangement
    Codeforces Round #454 D. Seating of Students
    浙大紫金港两日游
  • 原文地址:https://www.cnblogs.com/Key-Ky/p/9012814.html
Copyright © 2011-2022 走看看