Jupyter Notebooks offer a powerful and widely used platform for creating interactive scripts and journals. JupyterLab is the next-generation environment for Jupyter Notebooks that includes, among other things, a tabbed interface for multiple notebooks. Here, we will set these up to work with Matlab.
While Matlab’s Live Scripts offer a similar functionality, in my experience these are too buggy and unstable to be satisfactory. It takes a little work to set up Jupyter Notebooks to work with Matlab, but the payoff is worth it.
These instructions are taken from here, with some minor modifications and updates. They are written for a Mac or Linux operating system; the Windows instructions should be the same apart from obvious changes for directory changing commands and pathnames. More details on the Window installation are given in the original instructions.
If don’t already have it installed, you’ll need to install either the Anaconda or Miniconda Python distributions. Personally, I prefer the lean Miniconda distribution because it is a much faster install. Miniconda currently has versions for Python 2.7, 3.8, and 3.9. The version doesn’t particularly matter, so I chose the Python 3.9 version.
After Anaconda or Miniconda is installed, we’ll create a virtual environment, called
jmatlab, that specifies a suitable version of Python to work with Matlab. For the current release of Matlab at the time of this writing, R2021a, the most recent compatible version of Python is 3.8.8. Use
conda create -vv -n jmatlab python=3.8.8 jupyter
to create this environment. If you’re running a different version of Matlab, check this list to find the compatible versions of Python.
Now, activate the jmatlab environment
conda activate jmatlab
which puts you inside this environment, as your shell cursor probably indicates. All the remaining commands should be executed from within this environment.
Install JupyterLab using the command
conda install -c conda-forge jupyterlab
and then install the Matlab kernel with
pip install matlab_kernel python -m matlab_kernel install
pip command corrects a typo in the original instructions. To check that the kernel is installed correctly, use
jupyter kernelspec list
and you should see Matlab on the list of available kernels.
There is one more step that needs to be done on the Matlab side. Still inside the
jmatlab environment, type
cd /Applications/MATLAB_R2021a.app/extern/engines/python python setup.py install
which will allow the Matlab engine to be called from within a Python session.
Now, you’re ready to launch JupyterLab. Change to the directory you’d like to work in and type
or, alternatively, use
if you wish to start Jupyter Notebook without JupyterLab.
You should now be able to launch a Matlab notebook. You can do this by clicking on the “+” menu at the upper left if using JupyterLab, or, if you’re using Jupyter Notebook, from the “New” dropdown menu on the upper right.
To check that Matlab is working correctly, try entering
surf(peaks) at the command prompt and then pressing play. For this first command, there will be a long pause before the plot appears, but after that it should work more speedily.
The next time you want to use JupyterLab, you only need to activate the jmatlab environment with “
conda activate jmatlab” and then use “
jupyter-lab” to launch it.
Please contact me if you find any inaccuracies or omissions in these instructions.