Run Python commands from MATLAB Using the pyrun Function - MATLAB
Video Player is loading.
Current Time 0:00
Duration 3:18
Loaded: 4.99%
Stream Type LIVE
Remaining Time 3:18
 
1x
  • Chapters
  • descriptions off, selected
  • en (Main), selected
    Video length is 3:18

    Run Python commands from MATLAB Using the pyrun Function

    MATLAB® provides flexible, two-way integration with many programming languages, including Python®. Learn how to run individual Python commands interactively from MATLAB using the pyrun function. The demonstrated example includes Python commands that read from a web service and provides sample weather data for a user-defined city. The pyrun function is used to run those Python commands from MATLAB.

    Published: 23 Sep 2022

    MATLAB provides flexible, two-way integration with many programming languages, including Python. There are several ways to run your Python code in Matlab. In this video, you will see how to use Python to execute your Python code in MATLAB.

    How can pyrun help? Maybe you get some Python code from a colleague or on the web, and you want to use that code in MATLAB. Or you are working on a MATLAB application and want to use Python libraries, such as NumPy or Pandas. MATLAB span function can help you in such cases.

    With the help of pyrun, you can run individual patterns commands, even blocks of Python code to create variables for subsequent use in MATLAB. Look at the code on the right, having multiple Python statements in the same pyrun call. The sum of a and b is calculated using the Python interpreter. The result is stored in another Python variable, c, whose value is written as the MATLAB variable, d.

    Let us take an example to see how to use pyrun. In this example, we'll be using a sample where the data of a city from a web service at openweathermap.org. We already have a Python code, which takes city name and an API key as the user inputs, reads from the web service, passes the data collected, and stores it in the Python variable, out.

    To run this code in MATLAB, write each Python statement as a string. Since our code has multiple Python statements, create an array of such strings. We will call this a string array code.

    Use this string array code and the Python variable out as the inputs to the pyrun function to access the Python data in the variable out. We have already stored our API key in a MATLAB variable key. Provide this API key and the desired city name as the input arguments, and store the result in the variable output.

    Go ahead and run the script to get the sample where the data for the London City. Observe that the variable output has a Python dictionary data type. You can convert this into MATLAB struct by simply using the struct function.

    Python variables created using the pyrun function can be used for subsequent pyrun calls. If you go back to a first pyrun call, you can see that the temperature value with the variable name temp is in the Python dictionary main. And the description of the weather is in the Python list named weather. Use this information, and write the Python statements for taking out temperature and weather description from the variable out. Store the results in the MATLAB workspace by assigning variable names of your choice for subsequent usage in MATLAB.

    Now that you have extracted the desired Python data in MATLAB's workspace, you can use other MATLAB functions to manipulate this data. For example, you could use MATLAB's disp function to display the weather description and temperature. If you have a code saved as a Python script, you can use the pyrunfile function to run data script from MATLAB.

    To learn more about pyrun and pyrunfile, visit the MATLAB documentation. For more information on how to use MATLAB with Python, visit Using MATLAB with Python web page.

    Related Products