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.
Published: 14 Oct 2019
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.