Call Python from MATLAB for Wireless
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:
Use MATLAB to generate data sets to train the external language models by leveraging the Wireless Communications toolboxes.
Run simulations with your PyTorch model instantiations in MATLAB by modifying the PyTorch Wrapper Template to implement the recommended entry points.
Test the external language models in link-level and system-level simulations.
Integrate your work into Model Based Design workflows. For more information, see Deep Learning with Simulink (Deep Learning Toolbox).
Leverage software-defined radio (SDR) support to test your design with over-the-air (OTA) signals.
Import and deploy your system onto several possible platforms after qualifying your design. For more information, see Interoperability Between Deep Learning Toolbox, TensorFlow, PyTorch, and ONNX (Deep Learning Toolbox).
Try some sample AI applications in Example Workflows Using Python with MATLAB.
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.
After opening the External Languages panel, select the
Pythonoption from the language list at the upper left of the panel.
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.
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.

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.
Preprocess Data for AI-Based CSI Feedback Compression (5G Toolbox)
Preprocess Data for AI-Based CSI Prediction (5G Toolbox)
Train PyTorch Channel Prediction Models (5G Toolbox)
Train PyTorch Channel Prediction Models with Online Training (5G Toolbox)
Offline Training and Testing of PyTorch Model for CSI Feedback Compression (5G Toolbox)
Online Training and Testing of PyTorch Model for CSI Feedback Compression (5G Toolbox)
Apply Transfer Learning on PyTorch Model to Identify 5G and LTE Signals (5G Toolbox)
Verify Performance of 6G AI-Native Receiver Using MATLAB and PyTorch Coexecution (5G Toolbox)
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.

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™.