Using MATLAB with Python

MATLAB
19 Aug 202104:48

Summary

TLDRThis video tutorial explores the integration between MATLAB and Python, showcasing how to call Python functions from MATLAB and vice versa. It uses a weather data example from openweathermap.org, demonstrating the use of Python's 'weather.pi' module within MATLAB. The video guides through fetching and parsing weather data, converting Python dictionaries to MATLAB structs, and utilizing MATLAB functions like 'plot'. It also covers creating a MATLAB machine learning model for air quality prediction and how to expose this model to Python using the Engine API, allowing for a seamless workflow between the two programming environments.

Takeaways

  • 🔗 MATLAB provides two-way integration with Python, allowing for flexible interaction between the two programming environments.
  • 💻 The video uses Python 3.7 and demonstrates how to call Python functions from MATLAB and vice versa.
  • 🌐 Data from a web service, openweathermap.org, is used to illustrate the integration between MATLAB and Python.
  • 📚 MATLAB can call Python functions, create Python objects, and work with both base Python and installed packages.
  • 📊 The script showcases how to convert Python dictionaries into MATLAB structs and handle data type conversions.
  • 📈 MATLAB's `plot` function can be used to visualize data after converting it from Python types to MATLAB types.
  • 🔧 The video provides a step-by-step guide on how to use a machine learning model in MATLAB to predict air quality based on weather conditions.
  • 🛠️ MATLAB functions can be packaged to be used in Python, demonstrating the seamless integration between the two languages.
  • 📓 The video uses a Jupyter Notebook to demonstrate calling MATLAB from Python using the Engine API.
  • 🔚 The script concludes with instructions on shutting down the MATLAB engine started by the Engine API in the Jupyter Notebook.

Q & A

  • What is the purpose of the video on using MATLAB with Python?

    -The purpose of the video is to demonstrate how to integrate MATLAB with Python, showing how to call Python functions from MATLAB and how to call MATLAB functions from Python.

  • Which Python distribution is used in the video for integration with MATLAB?

    -Python 3.7 is used in the video for the integration with MATLAB.

  • What is the web service used in the example to get weather data?

    -The web service used in the example to get weather data is openweathermap.org.

  • What is the name of the Python module created by the speaker's colleague that interfaces with the weather web service?

    -The name of the Python module is 'weather.pi'.

  • How can one check their Python installation in MATLAB?

    -One can check their Python installation in MATLAB using the 'pyenv' command.

  • What is the basic syntax for calling a Python function from MATLAB?

    -The basic syntax for calling a Python function from MATLAB is 'py.<package or module name>.<function name>'.

  • How does MATLAB handle the conversion of Python dictionaries to MATLAB structs?

    -MATLAB automatically converts most of the data in a Python dictionary into MATLAB types, but for specific types like strings and date times, standard MATLAB functions like 'string', 'datetime' are used for conversion.

  • What function is used in the video to predict air quality based on weather conditions?

    -The 'predict air quality' function is used to predict air quality based on weather conditions.

  • How is the MATLAB function 'predict air quality' made accessible to Python?

    -The MATLAB function 'predict air quality' is made accessible to Python by packaging it into a function that can be called using the MATLAB Engine API for Python.

  • What is the first step to call MATLAB from Python using a Jupyter notebook?

    -The first step to call MATLAB from Python using a Jupyter notebook is to start a MATLAB engine in the background using the Engine API.

  • How is the MATLAB session that was started for communication with Python shut down in the video?

    -The MATLAB session started by the Engine API is shut down at the end of the Jupyter notebook session.

Outlines

00:00

🐍 Integrating MATLAB with Python

This video introduces the integration of MATLAB with Python, highlighting the two-way communication between the two programming languages. It discusses the compatibility of MATLAB with common Python distributions, using Python 3.7 as an example. The video demonstrates how to invoke Python functions from MATLAB and vice versa, utilizing a hypothetical Python module 'weather.pi' that interfaces with a web service to fetch and parse JSON data. The tutorial covers basic commands in MATLAB to check Python installation, specify Python versions, and call Python functions and objects. It also shows how to convert Python data types to MATLAB data types for further processing within MATLAB.

Mindmap

Keywords

💡MATLAB

MATLAB is a high-performance language for technical computing. It integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation. In the video, MATLAB is used to demonstrate how to interact with Python, showcasing its capability to call Python functions and how it can be used for data analysis and plotting.

💡Python

Python is a widely used high-level programming language known for its readability and versatility. It's extensively used in various domains, including web development, data analysis, artificial intelligence, and scientific computing. The video focuses on the integration of Python with MATLAB, highlighting how Python modules and functions can be utilized within MATLAB environments.

💡Integration

Integration in this context refers to the process of combining MATLAB and Python functionalities to work together seamlessly. The video demonstrates two-way integration: calling Python from MATLAB and vice versa, which is crucial for leveraging the strengths of both languages in a single workflow.

💡OpenWeatherMap

OpenWeatherMap is a web service that provides weather data. In the video, it's used as an example to show how to fetch and parse weather data using a Python module, which is then integrated with MATLAB for further analysis.

💡JSON

JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. The video script mentions that the 'get current weather' function in the weather module gets the current weather conditions in JSON format, which is then parsed into a Python dictionary.

💡Python Dictionary

A Python dictionary is a collection of key-value pairs. In the video, the parsed weather data from OpenWeatherMap is returned as a Python dictionary, which is then converted into a MATLAB struct for further processing within MATLAB.

💡MATLAB Struct

A MATLAB struct is a data structure that can store data of different types in named fields. The video explains how to convert a Python dictionary into a MATLAB struct, which allows for the use of MATLAB's built-in functions to manipulate the data.

💡Data Conversion

Data conversion refers to the process of transforming data from one format to another. The video illustrates how to convert Python data types, such as strings, lists, and arrays, into MATLAB data types like strings, cell arrays, and arrays, which is essential for using the data with MATLAB functions.

💡Machine Learning Model

A machine learning model is a system that learns from data and improves its predictions over time through algorithms. In the video, a MATLAB machine learning model is used to predict air quality based on weather conditions, demonstrating the application of MATLAB in predictive analytics.

💡Engine API

The Engine API in MATLAB allows for the creation and control of MATLAB engines from external programs, such as Python. The video shows how to start a MATLAB engine in the background for Python to communicate with, enabling the execution of MATLAB functions from within a Python environment.

💡Jupyter Notebook

A Jupyter Notebook is an open-source web application that allows creation and sharing of documents containing live code, equations, visualizations, and narrative text. In the video, a Jupyter Notebook is used to demonstrate how to call MATLAB functions from Python, showcasing the interactive nature of data analysis.

Highlights

Matlab offers two-way integration with Python, allowing for flexible interaction between the two programming languages.

Matlab is compatible with common Python distributions, and the video uses Python 3.7 for demonstration.

A Python module named 'weather.pi' is used to interact with a web service and parse JSON data.

The video demonstrates how to call Python functions from Matlab using the 'py' command.

Matlab can call Python functions, create Python objects, and work with both base Python and installed packages.

An example is provided on how to call the square root function from the Python 'math' package.

The 'get_current_weather' function in the 'weather' module fetches current weather data in JSON format.

The 'parse_current_json' function converts JSON data into a Python dictionary.

Python dictionaries can be converted into Matlab structs, with automatic type conversion for most data.

Matlab functions like 'double', 'string', and 'datetime' are used to convert Python types to Matlab types.

The 'getforecast' function is used to obtain a series of predicted weather conditions.

Python lists and arrays are converted into Matlab arrays using the 'double' function.

The video shows how to use a machine learning model in Matlab to predict air quality based on weather conditions.

The model is loaded from a '.map' file, current data is formatted, and the 'predict' method is called for air quality prediction.

A Matlab function 'predict_air_quality' is created for use in Python, encapsulating the model's prediction process.

The video demonstrates calling Matlab from Python using a Jupyter notebook and the Engine API.

Matlab functions can be called from Python after starting a Matlab session in the background.

The video concludes with shutting down the Matlab session started by the Engine API.

The Matlab documentation provides further information and examples on integrating Matlab and Python.

Transcripts

play00:00

hello and welcome to this video on using

play00:02

matlab with python

play00:04

matlab provides flexible two-way

play00:06

integration with many programming

play00:07

languages including python

play00:10

in this video we will see how to call

play00:11

python for matlab

play00:13

and how to call matlab from python

play00:15

matlab works with common python

play00:17

distributions

play00:18

for this video i will be using python

play00:20

3.7

play00:22

in this example we'll be using data from

play00:24

a web service at openweathermap.org

play00:28

let's suppose i have a colleague who

play00:29

works in python she has created a module

play00:32

called weather.pi

play00:33

that reads from the web service and

play00:35

parses the json data returned

play00:37

of course we can also do that in matlab

play00:39

but let's use this module as an example

play00:42

let's see how to call her python

play00:44

functions from matlab

play00:46

let's start with some basics in matlab

play00:49

you can check your python installation

play00:50

using the pi

play00:51

env command if you have multiple python

play00:54

versions installed

play00:56

you can use this command to specify

play00:57

which python you want to use

play01:00

matlab can call python functions and

play01:02

create python objects from base python

play01:05

from packages you have installed and

play01:07

from your own python code

play01:09

all python functions have the same basic

play01:11

syntax

play01:12

py followed by the package or module

play01:14

name

play01:15

followed by the function name here is an

play01:18

example call to the square root function

play01:20

in the python math package

play01:23

now let's see how to use my colleague's

play01:25

weather module we'll start by getting

play01:27

the data for today

play01:29

the get current weather function in the

play01:31

weather module gets the current weather

play01:33

conditions in json format

play01:35

the parse current json function then

play01:37

returns that data as a python dictionary

play01:40

let's convert the python dictionary into

play01:42

a matlab struct

play01:44

notice that most of the data gets

play01:46

automatically converted into matlab

play01:48

types

play01:49

however the city and current time are

play01:51

returned as python strings

play01:54

we'd like the city to be a matlab string

play01:56

and the current time to be a matlab date

play01:58

time

play01:59

we can do that using standard matlab

play02:01

functions like double

play02:03

string and date time now let's call the

play02:06

getforecast function which returns a

play02:08

series of predicted weather conditions

play02:10

over the next few days

play02:12

we can see that the current time is

play02:13

returned as a python list

play02:15

and the other values are returned as

play02:17

python arrays

play02:19

converting arrays or lists is also

play02:21

simple

play02:22

the matlab double function will convert

play02:24

the python array into a matlab array

play02:27

to convert the current time values we

play02:29

first convert the python list to a

play02:31

matlab cell array

play02:33

then convert each element of the cell

play02:35

array into a string

play02:36

then use the matlab datetime function to

play02:38

convert those strings into date times

play02:42

once we have the data as matlab data

play02:44

types we can call other matlab functions

play02:46

like plot

play02:48

for more information on the mapping

play02:50

between matlab data types and python

play02:52

data types

play02:53

search the matlab documentation for

play02:55

python data type

play02:57

now let's suppose i've created a machine

play02:59

learning model that takes a set of

play03:01

weather conditions

play03:02

and returns a prediction of the air

play03:03

quality i'll use my model to predict air

play03:06

quality for the current conditions

play03:09

let's see how that works there are three

play03:11

steps load the model from a map file

play03:14

take the current data from my python

play03:16

call and convert it into a format

play03:18

expected by the model call the predict

play03:20

method of the model to get the expected

play03:22

air quality

play03:24

my python colleague wants to make use of

play03:26

my air quality model in her python code

play03:29

to give this to my colleague i package

play03:32

it up

play03:32

into a function called predict air

play03:34

quality

play03:35

the function does the same three steps

play03:37

as above it loads the model

play03:39

converts the data and calls the model's

play03:42

predict method

play03:43

now we have a matlab function that uses

play03:45

the model to predict the air quality

play03:46

let's see how my colleague would use it

play03:48

in python

play03:50

we'll demonstrate how to call matlab

play03:52

from python using a jupyter notebook

play03:55

the first step is to use the engine api

play03:57

to start a matlab running in the

play03:59

background for python to communicate

play04:01

with

play04:02

once the matlab is running we can call

play04:04

any matlab function on the path

play04:07

now we can use the get current weather

play04:10

and parse current json functions

play04:11

from the weather module just like we did

play04:14

in matlab to get the current weather

play04:15

conditions

play04:17

then we can call the matlab function

play04:19

predict air quality

play04:20

to get the predictive results the last

play04:24

step is to shut down the matlab started

play04:26

by the engine api at the beginning of

play04:27

our notebook

play04:30

well that's it for this video on using

play04:32

matlow with python

play04:33

there's a lot more information in the

play04:35

matlab documentation including many

play04:37

examples

play04:38

just search the documentation for python

play04:41

thanks for watching

Rate This

5.0 / 5 (0 votes)

Связанные теги
MATLAB PythonPython integrationdata conversionmachine learningweather moduleJupyter NotebookAPI integrationMATLAB functionspredict air qualityprogramming tutorial
Вам нужно краткое изложение на английском?