Running MATLAB Code in Google Colaboratory with Ease
Learn how to leverage the power of MATLAB’s computing capabilities within the flexible environment of Google Colaboratory using Python scripts. This comprehensive guide will walk you through the proce …
Updated October 16, 2023
Learn how to leverage the power of MATLAB’s computing capabilities within the flexible environment of Google Colaboratory using Python scripts. This comprehensive guide will walk you through the process step-by-step, along with providing code samples for various applications.
Google Colaboratory, or simply “Colab”, is a cloud-based development environment that combines the benefits of both Jupyter Notebooks and Google’s computing infrastructure. This powerful platform makes it possible to utilize a wide range of programming languages and libraries within a single workspace. One of its most significant advantages lies in allowing users to combine the efficiency of MATLAB with Python for various data science applications.
In this article, we will focus on explaining how you can incorporate MATLAB into your Google Colab workflow seamlessly using Python scripting. By following these steps, you’ll be able to enjoy the best features of both languages, thus enhancing your overall productivity and improving the efficiency of your scientific computing tasks.
Before diving deep into our primary subject matter, let us ensure that we have all necessary prerequisites installed to facilitate a smooth experience with Google Colaboratory.
Set up a Google account or use an existing one. This is required for accessing the platform and making changes within your notebooks. You will also need to enable billing for your account, as some resources in Colab are billed at usage rates.
Install the Google Colaboratory (Colab) extension for the Chrome browser by visiting this link: Chrome Web Store Link. This will enable you to access and interact with Google Colab directly from your browser.
Download the MATLAB Runtime (MATLAB Compiler) for Python. You can find the appropriate installer for your operating system here: Download Link. This will allow you to execute MATLAB functions within your Python scripts, thereby unifying the two languages in Google Colab.
Creating a New Notebook in Google Colaboratory
- Launch Google Chrome and log in to your Google account.
- Click on the “Launch App” button from the Colab extension’s toolbar icon. This will open a new tab with access to Google Colab.
- You will be presented with a list of available Notebooks, if you have any prior ones saved. Otherwise, click on the “+ New Notebook” option in the upper left corner to create a fresh workspace for our current project.
Installing MATLAB Runtime and Dependencies
To use MATLAB features within your Python scripts, you must first install the MATLAB Runtime. This can be achieved by following these steps:
- Import the MATLAB Runtime library into your Colab notebook using the
!wget https://www.mathworks.com/downloads/compiler/2021b_py3/matlab-runtime-for-python-2021b-py3_linux64.whl -O matlab-runtime-for-python-2021b-py3_linux64.whl.
- Install the MATLAB Runtime using pip with the following command:
!pip install ./matlab-runtime-for-python-2021b-py3_linux64.whl
- Once installed, import the necessary libraries for your script:
import matlab.engine as engand
from matplotlib import pyplot as plt. We’ll be utilizing these libraries to establish a connection between Python and MATLAB within Google Colab.
Connecting and Executing MATLAB Code in Python Scripts
Now that you have successfully installed the MATLAB Runtime, it’s time to bridge the gap between Python and MATLAB within Google Colab. Let us break down this process step-by-step with a simple example:
Define a variable or function within your Python script using the
my_eng = eng.connect('matlab'). This establishes a connection between Python and MATLAB. The ‘matlab’ argument refers to the name under which you have installed MATLAB Runtime on your machine, so replace this with the appropriate path if needed.
Execute MATLAB code within your Python script using the MATLAB engine as follows:
my_eng.eval('your_MATLAB_code'). In this case, ‘your_MATLAB_code’ refers to any valid MATLAB command or function you wish to execute in the Colab environment.
For example, if you have a MATLAB script called
simple.m that performs basic arithmetic operations on vectors, you could use it within your Python script as follows:
import matlab.engine as eng
from matplotlib import pyplot as plt
# Establish connection between Python and MATLAB Runtime
my_eng = eng.connect('matlab')
# Execute MATLAB code within the Python script
result = my_eng.eval('simple.m')
By following these steps, you are now able to leverage the powerful computational capabilities of both Python and MATLAB within Google Colaboratory, thus enhancing your overall workflow efficiency.
In summary, incorporating MATLAB into your data science journey can significantly improve your scientific computing experience by harnessing the advantages of multiple programming languages. By utilizing Google Colaboratory’s flexible environment and Python scripting with MATLAB, you can bridge the gap between both platforms to create more efficient workflows for your various applications. With this comprehensive guide at hand, it is now easier than ever to explore the potential of combining Python and MATLAB within your computational endeavors on Google Colab.