Using Jupyter Notebooks and JupyterLab with Matlab

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. While Matlab’s Live Scripts offer a similar functionality, in my experience these are too buggy and unstable to be satisfactory. Here, we will set Jupyter up to work with Matlab.

As of January 2023, getting Jupyter to work with Matlab is now vastly easier than it was previously, for anyone not running Windows, thanks to the new Matlab Integration for Jupyter. The instructions that follow are taken from that source. Windows uses will have to work a little harder and should see these instructions

First we need to verify that Matlab is on the search path. Try which matlab, and if it is not found, follow appropriate steps for your system to add it to your path. Alternatively, on a Mac, assuming you are running the most recent version of Matlab, use sudo ln -s /Applications/MATLAB_R2023a.app/bin/matlab /usr/local/bin to create a symlink.

If don’t already have it installed, you’ll need to install either the Conda or Mamba package manager. Here there are some options. Anaconda is perhaps the most familiar, which comes in a leaner variety called Miniconda. Recently Mamba has emerged as a faster and fully compatible alternative to Conda, with Micromamba being a minimal version. These are all essentially equivalent so which you use is a matter of preference; personally I have been using Micromamba with no problems. At the time of this writing, the highest version of Python compatible with the Matlab Jupyter kernel is 3.10, so if you are installing Miniconda you should make sure to use that version.

After one of these package managers is installed, we’ll create a virtual environment, called jlab, that specifies a suitable version of packages to work with Matlab. These next two steps are not strictly necessary but help to keep the installation tidy.

conda create -vv -n jlab python=3.10 jupyterlab -y
conda activate jlab

with conda being replaced with mamba or micromamba depending on the package manager being used. Then we’ll install the Matlab Jupyter kernel with

python3 -m pip install jupyter-matlab-proxy

Now, change to the directory you’d like to work in and type

jupyter lab

or, alternatively, use

jupyter notebook

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 “MATLAB Kernel” icon if using JupyterLab, or, if you’re using Jupyter Notebook, from the “New” dropdown menu on the upper right. Your first time working with Matlab, you will probably be prompted to click on “Open MATLAB” and then need to enter your MathWorks login information.

Note that there is a special consideration not currently mentioned in the documentation, which is the Matlab Jupyter kernel doesn’t work with Jupyter password authentication. If you find that your kernel dies immediately after starting, it’s probably because you have password authentication set up in Jupyter. Follow these steps to delete jupyter_server_config.json or jupyter_notebook_config.json, which reverts to token authentication and should fix the problem.

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 jlab environment with “conda activate jlab” followed by “jupyter lab”, again substituting for conda as appropriate.

The JupyterLab interface is fairly self-evident, but more details can be found in its documentation. A great reference for working with the Markdown format of the text cells can be in this tutorial.

Please contact me if you find any inaccuracies or omissions in these instructions.