Функции matlab в python

Call MATLAB from Python

The MATLAB Engine API for Python provides a package for Python to call MATLAB as a computational engine. The engine supports the reference implementation (CPython). For supported version information, see Versions of Python Compatible with MATLAB Products by Release.

Engine applications require an installed version of MATLAB; you cannot run the MATLAB engine on a machine that only has the MATLAB Runtime.

Functions

Python Functions

matlab.engine.start_matlab Start MATLAB Engine for Python
matlab.engine.find_matlab Find shared MATLAB sessions to connect to MATLAB Engine for Python
matlab.engine.connect_matlab Connect shared MATLAB session to MATLAB Engine for Python

MATLAB Functions

matlab.engine.shareEngine Convert running MATLAB session to shared session
matlab.engine.engineName Return name of shared MATLAB session
matlab.engine.isEngineShared Determine if MATLAB session is shared

Classes

Python Classes

matlab.engine.MatlabEngine Python object using MATLAB as computational engine within Python session
matlab.engine.FutureResult Results of asynchronous call to MATLAB function stored in Python object

Topics

Installing

  • System Requirements for MATLAB Engine API for Python
    What you need to write and build MATLAB engine applications for Python.
  • Install MATLAB Engine API for Python
    To start MATLAB engine within a Python session, install the engine API as a Python package.
    • Python Setup Script to Install MATLAB Engine API
    • Install MATLAB Engine API for Python in Nondefault Locations

    Getting Started

    • Get Started with MATLAB Engine API for Python
      MATLAB Engine API for Python provides a Python package named matlab that enables you to call MATLAB functions from Python.
    • Start and Stop MATLAB Engine for Python
      Options for starting the MATLAB Engine for Python.
    • Call MATLAB Functions from Python
      How to return an output argument from a MATLAB function. How to read multiple outputs from a function. What to do when the MATLAB function does not return an output argument.
    • Get Help for MATLAB Functions from Python
      From Python, you can access supporting documentation for all MATLAB functions.

    Session Management

    • Connect Python to Running MATLAB Session
      How to connect the MATLAB Engine for Python to a shared MATLAB session that is already running on your local machine.

    Using MATLAB Workspace

    • Use MATLAB Engine Workspace in Python
      This example shows how to add variables to the MATLAB engine workspace in Python.

    Data Exchange and Mapping

    • Use MATLAB Arrays in Python
      This example shows how to create a MATLAB array in Python and pass it as the input argument to the MATLAB sqrt function.
    • MATLAB Arrays as Python Variables
      The matlab Python module provides array classes to represent arrays of MATLAB numeric types as Python variables so that MATLAB arrays can be passed between Python and MATLAB.
    • Pass Data to MATLAB from Python
      When you pass Python data as input arguments to MATLAB functions, the MATLAB Engine for Python converts the data into equivalent MATLAB data types.
    • Handle Data Returned from MATLAB to Python
      When MATLAB functions return output arguments, MATLAB Engine API for Python converts the data into equivalent Python data types.
    • Use MATLAB Handle Objects in Python
      This example shows how to create an object from a MATLAB handle class and call its methods in Python.
    • Default Numeric Types in MATLAB and Python
      MATLAB stores all numeric values as double-precision floating point numbers by default.

    Calling MATLAB Functions

    • Call User Scripts and Functions from Python
      This example shows how to call a MATLAB script to compute the area of a triangle from Python.
    • Sort and Plot MATLAB Data from Python
      This example shows how to sort data about patients into lists of smokers and nonsmokers in Python and plot blood pressure readings for the patients with MATLAB.
    • 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.
    • Redirect Standard Output and Error to Python
      This example shows how to redirect standard output and standard error from a MATLAB function to Python StringIO objects.

    Troubleshooting

    MATLAB Engine API for Python does not support these features.

    When a MATLAB function raises an error, the MATLAB Engine for Python stops the function and catches the exception raised by MATLAB.

    Источник

    Функции matlab в python

    Use MATLAB ® Engine API for Python ® to call any MATLAB function on the MATLAB path.

    If the MATLAB function is not on the path, you can call it from the current folder. For example, to call MATLAB function myFnc in folder myFolder , type:

    import matlab.engine eng = matlab.engine.start_matlab() eng.cd(r'myFolder', nargout=0) eng.myFnc()

    Return Output Argument from MATLAB Function

    You can call any MATLAB function directly and return the results to Python. For example, to determine if a number is prime, use the engine to call the isprime function.

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

    Return Multiple Output Arguments from MATLAB Function

    When you call a function with the engine, by default the engine returns a single output argument. If you know that the function can return multiple arguments, use the nargout argument to specify the number of output arguments.

    To determine the greatest common denominator of two numbers, use the gcd function. Set nargout to return the three output arguments from gcd .

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

    Return No Output Arguments from MATLAB Function

    Some MATLAB functions return no output arguments. If the function returns no arguments, set nargout to 0.

    Open the MATLAB Help browser from Python.

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

    The MATLAB doc function opens the browser, but does not return output arguments. If you do not specify nargout=0 , the engine raises an error.

    Stop Execution of Function

    To stop execution of a MATLAB function press Ctrl+C. Control returns to Python.

    Use Function Names for MATLAB Operators

    You can use a MATLAB operator in Python by calling the equivalent function. For a list of operators and associated function names, see MATLAB Operators and Associated Functions. For example, to add two numbers, use the plus function instead of the + operator.

    import matlab.engine eng = matlab.engine.start_matlab() a = 2 b = 3 eng.plus(a,b)

    See Also

    Источник

    Call MATLAB from Python

    The MATLAB Engine API for Python provides a package for Python to call MATLAB as a computational engine. The engine supports the reference implementation (CPython). For supported version information, see Versions of Python Compatible with MATLAB Products by Release.

    Engine applications require an installed version of MATLAB; you cannot run the MATLAB engine on a machine that only has the MATLAB Runtime.

    Functions

    Python Functions

    matlab.engine.start_matlab Start MATLAB Engine for Python
    matlab.engine.find_matlab Find shared MATLAB sessions to connect to MATLAB Engine for Python
    matlab.engine.connect_matlab Connect shared MATLAB session to MATLAB Engine for Python

    MATLAB Functions

    matlab.engine.shareEngine Convert running MATLAB session to shared session
    matlab.engine.engineName Return name of shared MATLAB session
    matlab.engine.isEngineShared Determine if MATLAB session is shared

    Classes

    Python Classes

    matlab.engine.MatlabEngine Python object using MATLAB as computational engine within Python session
    matlab.engine.FutureResult Results of asynchronous call to MATLAB function stored in Python object

    Topics

    Installing

Оцените статью