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.

Note that, as of this update [October 2022], I have not been able to get this to work on a Mac running the new M1 chip, presumably related to the fact that Matlab is not yet running natively on this architecture.

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 `jlab`

, that specifies a suitable version of Python to work with Matlab. For the current release of Matlab at the time of this writing [updated October 2022], R2022b, the most recent compatible version of Python is 3.10. Use

```
conda create -vv -n jlab python=3.10 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 jlab environment

```
conda activate jlab
```

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
```

where the `pip`

command corrects a typo in the original instructions. If you run into an error on the second line, use “`sudo python -m matlab_kernel install`

”.

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. If you are not running a Mac with the new M1 chips, still inside the `jlab`

environment, type

```
cd /Applications/MATLAB_R2022b.app/extern/engines/python
python -m pip install matlabengine
```

which will allow the Matlab engine to be called from within a Python session. Note that for older versions of Matlab, the second line might need to be replaced with “`python setup.py install`

”.

Now, you’re ready to launch JupyterLab. 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 “+” 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 jlab environment with “`conda activate jlab`

” and then use “`jupyter-lab`

” to launch it.

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.