主要内容

Call Python from MATLAB for Wireless

Since R2025a

Before you can run simulations that use PyTorch® or TensorFlow™ models, you must first configure your computer to call Python® from MATLAB®. To install a supported python implementation on your computer, see Configure Your System to Use Python.

Once you have configured your computer and your MATLAB environment, you can then:

Set Up MATLAB Environment

Use the External Languages panel in MATLAB to create a Python virtual environment that calls the requirements file noted in the example you will run. To avoid library conflicts, you must update the virtual environment each time you run a different example.

  1. After opening the External Languages panel, select the Python option from the language list at the upper left of the panel.

    A screenshot of the External Languages panel in MATLAB showing Python environments. The selected Python environment is version 3.12 located at "C:\Users\user\AppData\Local\Programs\Python\Python312\python.exe". Under "All Python Environments," two base environments are listed: Python 3.12 and Python 3.11, each with their respective file paths. There is also a "Virtual Environments" section showing an environment named "sandbox01 - 3.12" located at "C:\Users\user\sandbox01\Scripts\pythonw.exe".

  2. To add your selected Python environment for use with any of the examples in the next section, click the Add button , and create a virtual environment.

  3. In the Create Virtual Environment dialog box, fill in the fields for the base environment, virtual environment name, location, and requirements file. These fields must match the values defined in the example that you want to run. Also select the Set as selected environment box. A window titled "Create Virtual Environment" It contains dropdown and text fields for selecting a base environment, entering a name, and specifying a location. There are radio buttons to choose library installation options: "None", "All from base environment", or "From requirements file" (with an additional input field for the requirements file). There is also a checkbox labeled "Set as selected environment". At the bottom, there are two buttons: "Create" (disabled until necessary fields are completed) and "Cancel". Small folder icons appear next to the location and requirements file fields, allowing the user to browse for a file rather than type in a path.

For more information on the External Languages panel, see Manage Python Environments Using External Languages Panel.

Example Workflows Using Python with MATLAB

These examples show how to use PyTorch models in AI for wireless applications.

Each example includes a requirements supporting file to identify the Python libraries that you need and their specific versions. You can use the requirements files with the pip command line program to install all the required libraries. For more information, see https://pip.pypa.io/en/stable/user_guide/.

To simplify environment reconfiguration for initial simulation runs, the application examples have ExecutionMode set to "OutOfProcess" for the pyenv function. Running Python functions out-of-process is a debug mode that allows you to adjust and optimize the environment settings. The out-of-process mode introduces overhead between MATLAB and Python that increases run time. For more information, see Out-of-Process Execution of Python Functionality.

Once you optimize the environment settings, switch the ExecutionMode setting to InProcess to avoid overhead between the MATLAB and Python processes and decrease run time. Use the gear icon on the External Languages panel to switch between the process mode.

The MATLAB External Lanaguages panel with gear dropdown selceted to switch process mode.

Note

After first loading pyenv with ExecutionMode set to "InProcess", you must restart MATLAB to change the execution mode to "OutOfProcess" and to change the Python version.

To debug Python code from MATLAB, see How can I debug Python code using MATLAB's Python Interface and Visual Studio Code in MATLAB Answers™.

See Also

|

Topics