In this video, you will learn how to call MATLAB code from Python
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 was 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 the microphone, detect text from the audio signal and then classify the text using our sentiment analysis model
Suppose I'm doing all my development in Python, but my colleague already has MATLAB code to perform sentiment analysis on text
Instead of rewriting code or finding a new solution, I want to still use the MATLAB code for sentiment analysis and do the rest of my work in Python
There are two approaches for calling MATLAB code from Python
The first is to use the MATLAB engine API for Python, which requires a MATLAB install
The second is to use MATLAB Compiler SDK to compile a Python package that does not require users to have a MATLAB install
Let's first see our MATLAB code
This is a simple test script where I can write a sentence and pass it into my sentiment analysis function
The idea is to call the same function from the rest of our code that's written in Python
As you can see, the predicted sentiment is neutral, which seems correct
If you're wondering what's in this function, I'm using a built-in sentiment analysis Algorithm called VADER from Text Analytics Toolbox
Now, let's go to Python
I have a 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 a "speechToText" function
This listens to an audio device, such as a microphone, and returns the detected text and a Boolean flag indicating whether the detection was successful
Here is a notebook showing the process using the MATLAB Engine API for Python
Recall that this approach requires a user to have a licensed installation of MATLAB on their machine
I will first import the speech recognition and MATLAB Engine modules
Then, I will start a new MATLAB process which returns an object for communicating with this process
Let's use the built-in "speechToText" function in our module to listen to my microphone and display the detected text
For example, "I went to my favorite restaurant and had a delicious meal"
Now that we have the detected text, it's time to call the MATLAB code
With this line of code. I'm instructing MATLAB to call the "sentimentAnalysisVADER" function
My inputs are the detected text, and the number of output arguments I want to assign - in this case, two
As you can see from the printed outputs. the predicted sentiment is positive
Now that we're done running MATLAB code, let's shut down the session
Let's explore the second approach which is to compile a Python package from our MATLAB code
To do this, I will go to the Apps tab in MATLAB and Open the Library Compiler app
I will select an output type of "Python Package" and then add my sentiment analysis and then add my "sentimentAnalysisVADER" function for export
You can fill in the rest of the settings with useful information and documentation
But I will jump ahead to the "Samples" section
Here is where you can provide sample code that calls your exported functions, and the Library Compiler app will automatically generate equivalent Python samples to show you how to call the package
Let's add our test script from earlier
Now, we can select "Package" to create our standalone component
When this is done, I can look at the output folder, and in the "Samples" folder, you can see the Python equivalent of the MATLAB test script that I provided
Going back to Python, here's my notebook showing the compiled package approach
Users can run these compiled packages with the MATLAB Runtime, which you can directly share with your users, or they can download it from the MathWorks website
Note that, before running this code, both the compiled package and MATLAB Runtime need to be installed
Please refer to the documentation for steps on how to do this
I will first import the speech recognition module and the package we just generated
Then, I will call the "initialize" function of the compiled package to start a MATLAB Runtime
Let's use the built-in "speechToText" function in our module to listen to my microphone and display the detected text
For example, "I wanted to go outside but the weather is terrible"
Now that we have the detected text, we can call the "sentimentAnalysisVADER" function
My inputs are the detected text, and the number of output arguments that I want to assign - in this case, two
As you can see from the printed outputs, the predicted sentiment is negative
And finally, let's terminate the MATLAB Runtime
That concludes our example
To summarize, let's talk about why you would want to call MATLAB from Python
The workflow we showed fits if you're already working in Python and want to use MATLAB to solve part of the problem
This could be either that you have existing mountain code that you would like to reuse or you need to access functionality that is only available in MATLAB and its various toolboxes and add-ons
In addition, you can use MATLAB Compiler SDK to compile Python packages that can be shared with users that do not have MATLAB installed
To learn more, check out the resources below
Also, make sure to watch our other video on how to call Python from MATLAB