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. These can now be set up to work with Matlab thanks to the new Matlab Integration for Jupyter. The instructions that follow are taken from that source.

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. On a Mac, assuming you are running the most recent version of Matlab, use sudo ln -s /Applications/MATLAB_R2024a.app/bin/matlab /usr/local/bin to create a symlink. This step is omitted from the installation instructions, but is found here as the recommended method to add Matlab to your path.

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.

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.11 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

python -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 does not work with Jupyter password authentication, see this discussion. If you find that your kernel can’t connect or 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. On a Mac, when prompted to enter the token, you can find this by trying jupyter notebook list at the command line.

To check that Matlab is working correctly, try entering surf(peaks) in a cell 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.

If you’re using VSCode, you can run Matlab by first starting Matlab in a Jupyter server as described above. Then in VSCode choose “Select Kernel —> Select Another Kernel… —> Existing Jupyter Server…”, enter the Jupyter server URL, e.g. http://localhost:8888, and finally when prompted for a password use the token associated with the Jupyter server.

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.