Skip to content
MathWorks - Mobile View
  • Sign In to Your MathWorks AccountSign In to Your MathWorks Account
  • Access your MathWorks Account
    • My Account
    • My Community Profile
    • Link License
    • Sign Out
  • Products
  • Solutions
  • Academia
  • Support
  • Community
  • Events
  • Get MATLAB
MathWorks
  • Products
  • Solutions
  • Academia
  • Support
  • Community
  • Events
  • Get MATLAB
  • Sign In to Your MathWorks AccountSign In to Your MathWorks Account
  • Access your MathWorks Account
    • My Account
    • My Community Profile
    • Link License
    • Sign Out

Videos and Webinars

  • MathWorks
  • Videos
  • Videos Home
  • Search
  • Videos Home
  • Search
  • Contact sales
  • Trial software
3:04 Video length is 3:04.
  • Description
  • Full Transcript
  • Related Resources

How to Call Python from MATLAB

This video shows how to call Python® code from MATLAB® using a sentiment analysis example. This example consists of listening to audio through a microphone, detecting text from speech, and using a pretrained machine learning model to predict the sentiment (positive, negative, or neutral) of the detected text. In the example, MATLAB is first used to record audio data from a microphone. A user-defined Python module is then used to detect text from the audio signal. This text is then returned to MATLAB to continue performing the sentiment analysis.

In this video, you will learn how to call Python code from MATLAB.

To do this, we will use a sentiment analysis example. Suppose a person says that was the best concert they ever attended. A sentiment analysis algorithm will look at this text and output what it thinks is the intent – or the sentiment – of the person. In this case, it should predict that this is a positive statement. On the other hand, if the person says they dropped their ice cream on the floor and they are sad, then the algorithm should predict a negative sentiment.

The sentiment analysis program might look like this. We listen to an audio source like a microphone, detect text from the audio signal, and then classify the text using our sentiment analysis model. Suppose I am doing all my development in MATLAB, but my colleague already has Python code to perform the speech-to-text conversion. Instead of rewriting code or finding a new solution, I want to still use the Python code for speech-to-text and do the rest of my work in MATLAB.

Let’s first see our Python code. I have a Python module that uses the SpeechRecognition package, and particularly the PocketSphinx software from Carnegie Mellon University, to recognize text from speech. In this package, my colleague provided an “audioToText” function. This accepts an audio signal, along with its sampling frequency and channel width, and returns the detected text and a Boolean flag indicating whether the detection was successful.

Going to MATLAB, here is our Live Script showing the entire sentiment analysis process.

First, I will use the “pyversion” function to see which version of the Python interpreter is being picked up by MATLAB. For me, it’s picking up Python 3.7. Refer to the documentation to see which versions are supported for your release of MATLAB, and how to set additional options. In this step, I am also adding my colleague’s Python module to the Python path.

Next, I will use the built-in “audiorecorder” function in MATLAB to listen to my microphone and display the resulting audio signal. Let’s listen for 5 seconds and give the program a sentence to test. For example, “I went to my favorite restaurant and had a delicious meal”.

Now that we have the audio data, it’s time to call the Python code. With this line of code, I am instructing MATLAB to call the “audioToText” function in the “mySpeechRecognizer” Python package. I can then extract the output data from the returned list of Python outputs.

We have the recognized text in MATLAB. Finally, I will call a MATLAB function to perform sentiment analysis. As you can see, the predicted sentiment is positive, which seems correct. If you’re wondering what’s in this function, I am using a built-in sentiment analysis algorithm called VADER from Text Analytics Toolbox.

That concludes our example. To summarize, let’s talk about why you would want to call Python from MATLAB. The workflow we showed fits if you are already working in MATLAB and want to use Python to solve part of the problem. This could be either that you have existing Python code you would like to reuse, or you need to access functionality that is only available in Python.

To learn more, check out the resources below. Also, make sure to watch our other video on how to call MATLAB from Python.

Related Products

  • MATLAB

Download code
Using MATLAB with Python
System and configuration requirements to call Python from MATLAB
Call user-defined Python modules in MATLAB
Train a sentiment classifier
Sequence classification using deep learning

Bridging Wireless Communications Design and Testing with MATLAB

Read white paper

Cheat Sheets for Using MATLAB with Python

Get cheat sheets

Feedback

Featured Product

MATLAB

  • Request Trial
  • Get Pricing

Up Next:

2:09
How to Store a Series of Vectors from a for Loop

Related Videos:

18:43
Emotion Recognition of Call Center Conversations
31:29
MATLAB: Tips and Tricks
39:07
MATLAB Meets MINDSTORMS: How to Control LEGO NXT Robots...
3:05
Technical Computing with MATLAB, Part 2: Importing from...

View more related videos

MathWorks - Domain Selector

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

  • Switzerland (English)
  • Switzerland (Deutsch)
  • Switzerland (Français)
  • 中国 (简体中文)
  • 中国 (English)

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
    • 简体中文Chinese
    • English
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

  • Contact sales
  • Trial software

MathWorks

Accelerating the pace of engineering and science

MathWorks is the leading developer of mathematical computing software for engineers and scientists.

Discover…

Explore Products

  • MATLAB
  • Simulink
  • Student Software
  • Hardware Support
  • File Exchange

Try or Buy

  • Downloads
  • Trial Software
  • Contact Sales
  • Pricing and Licensing
  • How to Buy

Learn to Use

  • Documentation
  • Tutorials
  • Examples
  • Videos and Webinars
  • Training

Get Support

  • Installation Help
  • MATLAB Answers
  • Consulting
  • License Center
  • Contact Support

About MathWorks

  • Careers
  • Newsroom
  • Social Mission
  • Customer Stories
  • About MathWorks
  • Select a Web Site United States
  • Trust Center
  • Trademarks
  • Privacy Policy
  • Preventing Piracy
  • Application Status

© 1994-2022 The MathWorks, Inc.

  • Facebook
  • Twitter
  • Instagram
  • YouTube
  • LinkedIn
  • RSS

Join the conversation