Using MATLAB with Python - MATLAB
Video Player is loading.
Current Time 0:00
Duration 4:48
Loaded: 1.92%
Stream Type LIVE
Remaining Time 4:48
 
1x
  • Chapters
  • descriptions off, selected
  • en (Main), selected
    Video length is 4:48

    Using MATLAB with Python

    MATLAB® provides flexible, two-way integration with many programming languages including Python. This allows different teams to work together and use code written in MATLAB with code written in Python. In this video, you will learn how to call Python functions from MATLAB and how to call MATLAB functions from Python.

    Published: 28 Feb 2022

    Hello, and welcome to this video on using MATLAB with Python. MATLAB provides flexible two-way integration with many programming languages, including Python. In this video, we will see how to call Python from MATLAB and how to call MATLAB from Python.

    MATLAB works with common Python distributions. For this video, I will be using Python 3.7.

    In this example, we'll be using data from a web service at openweathermap.org. Let's suppose I have a colleague who works in Python. She has created a module called weather.pi that reads from the web service and parses the JSON data returned. Of course, we can also do that in MATLAB. But let's use this module as an example.

    Let's see how to call her Python functions from MATLAB. Let's start with some basics. In MATLAB, you can check your Python installation using the pyenv command. If you have multiple Python versions installed, you can use this command to specify which Python you want to use.

    MATLAB can call Python functions and create Python objects from base Python from packages you have installed and from your own Python code. All Python functions have the same basic syntax, py followed by the package or module name, followed by the function name. Here is an example called to the square root function in the Python math package.

    Now let's see how to use my colleague's weather module. We'll start by getting the data for today. The get_current_weather function in the weather module gets the current weather conditions in JSON format. The parsed current JSON function then returns that data as a Python dictionary.

    Let's convert the Python dictionary into a MATLAB struct. Notice that most of the data gets automatically converted into MATLAB types. However, the city and current time are returned as Python strings. We'd like the city to be a MATLAB string and the current time to be a MATLAB date-time. We can do that using standard MATLAB functions like double, string, and datetime.

    Now let's call the get_forecast function, which returns a series of predicted weather conditions over the next few days. We can see that the current time is returned as a Python list. And the other values are returned as Python arrays.

    Converting arrays or lists is also simple. The MATLAB double function will convert the Python array into a MATLAB array. To convert the current time values, we first convert the Python list to a MATLAB cell array then convert each element of the cell array into a string then use the MATLAB datetime function to convert those strings into date-times.

    Once we have the data as MATLAB data types, we can call other MATLAB functions, like plot. For more information on the mapping between MATLAB data types and Python data types, search the MATLAB documentation for Python data type.

    Now let's suppose I've created a Machine Learning model that takes a set of weather conditions and returns a prediction of the air quality. I'll use my model to predict air quality for the current conditions. Let's see how that works.

    There are three steps, load the model from a .mat file, take the current data from my Python call, and convert it into a format expected by the model. Call the predict method of the model to get the expected air quality.

    My Python colleague wants to make use of my air quality model in her Python code. To give this to my colleague, I package it up into a function called predictAirQuality. The function does the same three steps as above. It loads the model, converts the data, and calls the model's predict method.

    Now we have a MATLAB function that uses the model to predict the air quality. Let's see how my colleague would use it in Python.

    We'll demonstrate how to call MATLAB from Python using a Jupyter notebook. The first step is to use the engine API to start a MATLAB running in the background for Python to communicate with. Once the MATLAB is running, we can call any MATLAB function on the path.

    Now we can use the get_current_weather and parse current JSON functions from the weather module, just like we did in MATLAB, to get the current weather conditions. Then we can call the MATLAB function, predictAirQuality, to get the predicted results. The last step is to shut down the MATLAB started by the engine API at the beginning of our notebook.

    Well, that's it for this video on using MATLAB with Python. There's a lot more information in the MATLAB documentation including many examples. Just search the documentation for Python. Thanks for watching.

    Related Products

    View more related videos