After creating a custom Matlab kernel, you'll see the option for a `Matlab` kernel in the "New" drop down when starting a new notebook. The Matlab engine set up here will reference the version of Matlab used during the `build` process. If you'd like to upgrade to a newer version, you'll need to repeat building the engine and moving that into the `site-packages` directory as above.

Instructions

You can install a Matlab kernel by running the following at the command line:

# The Matlab engine supports recent python 2 and 3. Here I will use a known working version--python 3.6.4

# First, set up your virtual environment

module load python/3.6.4

virtualenv --system-site-packages my_personal_env

source my_personal_env/bin/activate

pip install jupyter matlab_kernel pymatbridge

pip install --upgrade --force jupyter-console

 

# Next, build the matlab engine 
# Initially build the engine to `/usr/workspace/$USER/lib/matlab`

cd /collab/usr/gapps/estk/matlab-2018b/extern/engines/python

python3 setup.py build -b /usr/workspace/$USER

# move the matlab engine into your virtual environmen
mv /usr/workspace/$USER/lib/matlab $HOME/my_personal_env/lib/python3.6/site-packages/matlab

# you may want to remove the now-empty /lib dir via
rmdir /usr/workspace/$USER/lib



# Install the matlab kernel

# The following produces the file `.local/share/jupyter/kernels/matlab/kernel.json`
python3 -m matlab_kernel install --prefix=$HOME/.local/

# Edit your kernel.json file to replace `python` with the path to the python binary from your virtual environment. For example, update the kernel.json text from
```
{"argv": ["python", "-m", "matlab_kernel", "-f", "{connection_file}"], "display_name": "Matlab", "language": "matlab", "mimetype": "text/x-octave", "name": "matlab"}
```

to


```
{"argv": ["$HOME/my_personal_env/bin/python3", "-m", "matlab_kernel", "-f", "{connection_file}"], "display_name": "Matlab", "language": "matlab", "mimetype": "text/x-octave", "name": "matlab"}
```

 

Example

Below is an example installation showing some of the outputs you'll expect to see in following the steps above:

janeh@flash21:/usr/workspace/janeh$ module load python/3.6.4

janeh@flash21:/usr/workspace/janeh$ virtualenv --system-site-packages my-python3x6-matlab-env

Using base prefix '/collab/usr/gapps/python/build/spack-toss3.2/opt/spack/linux-rhel7-x86_64/gcc-4.9.3/python-3.6.4-zx542u32lunrjpa7obuhte2oqanmbfex'

New python executable in /usr/WS1/janeh/my-python3x6-matlab-env/bin/python3.6

Also creating executable in /usr/WS1/janeh/my-python3x6-matlab-env/bin/python

Installing setuptools, pip, wheel...;done.

janeh@flash21:/usr/workspace/janeh$ source my-python3x6-matlab-env/bin/activate

(my-python3x6-matlab-env) janeh@flash21:/usr/workspace/janeh$ pip install jupyter matlab_kernel pymatbridge

Collecting jupyter

(…)

Successfully installed Send2Trash-1.8.0 argon2-cffi-21.3.0 argon2-cffi-bindings-21.2.0 async-generator-1.10 attrs-21.4.0 bleach-4.1.0 cffi-1.15.1 dataclasses-0.8 defusedxml-0.7.1 entrypoints-0.4 importlib-metadata-4.8.3 ipykernel-5.5.6 ipython-genutils-0.2.0 ipywidgets-7.7.1 jedi-0.17.2 jsonschema-3.2.0 jupyter-1.0.0 jupyter-client-7.1.2 jupyter-console-6.4.3 jupyter-core-4.9.2 jupyterlab-pygments-0.1.2 jupyterlab-widgets-1.1.1 matlab-kernel-0.17.1 metakernel-0.28.2 mistune-0.8.4 nbclient-0.5.9 nbconvert-6.0.7 nbformat-5.1.3 nest-asyncio-1.5.5 notebook-6.4.10 pandocfilters-1.5.0 parso-0.7.1 prometheus-client-0.14.1 prompt-toolkit-3.0.30 pycparser-2.21 pygments-2.12.0 pymatbridge-0.5.2 pyrsistent-0.18.0 pyzmq-23.2.0 qtconsole-5.2.2 qtpy-2.0.1 six-1.16.0 terminado-0.12.1 testpath-0.6.0 tornado-6.1 typing-extensions-4.1.1 webencodings-0.5.1 widgetsnbextension-3.6.1 wurlitzer-3.0.2 zipp-3.6.0

(my-python3x6-matlab-env) janeh@flash21:/usr/workspace/janeh$ pip install --upgrade --force jupyter-console

Collecting jupyter-console

(…)

      Successfully uninstalled jupyter-console-6.4.3

Successfully installed backcall-0.2.0 decorator-5.1.1 entrypoints-0.4 ipykernel-5.5.6 ipython-7.16.3 ipython-genutils-0.2.0 jedi-0.17.2 jupyter-client-7.1.2 jupyter-console-6.4.3 jupyter-core-4.9.2 nest-asyncio-1.5.5 parso-0.7.1 pexpect-4.8.0 pickleshare-0.7.5 prompt-toolkit-3.0.30 ptyprocess-0.7.0 pygments-2.12.0 python-dateutil-2.8.2 pyzmq-23.2.0 setuptools-59.6.0 six-1.16.0 tornado-6.1 traitlets-4.3.3 wcwidth-0.2.5

(my-python3x6-matlab-env) janeh@flash21:/usr/workspace/janeh$ cd /collab/usr/gapps/estk/matlab-2018b/extern/engines/python

(my-python3x6-matlab-env) janeh@flash21:/collab/usr/gapps/estk/matlab-2018b/extern/engines/python$ python3 setup.py build -b /usr/workspace/janeh

running build

running build_py

creating /usr/workspace/janeh/lib

creating /usr/workspace/janeh/lib/matlab

copying dist/matlab/__init__.py -> /usr/workspace/janeh/lib/matlab

copying dist/matlab/mlarray.py -> /usr/workspace/janeh/lib/matlab

copying dist/matlab/mlexceptions.py -> /usr/workspace/janeh/lib/matlab

creating /usr/workspace/janeh/lib/matlab/engine

copying dist/matlab/engine/matlabengine.py -> /usr/workspace/janeh/lib/matlab/engine

copying dist/matlab/engine/basefuture.py -> /usr/workspace/janeh/lib/matlab/engine

copying dist/matlab/engine/enginehelper.py -> /usr/workspace/janeh/lib/matlab/engine

copying dist/matlab/engine/engineerror.py -> /usr/workspace/janeh/lib/matlab/engine

copying dist/matlab/engine/futureresult.py -> /usr/workspace/janeh/lib/matlab/engine

copying dist/matlab/engine/__init__.py -> /usr/workspace/janeh/lib/matlab/engine

copying dist/matlab/engine/matlabfuture.py -> /usr/workspace/janeh/lib/matlab/engine

copying dist/matlab/engine/fevalfuture.py -> /usr/workspace/janeh/lib/matlab/engine

copying dist/matlab/engine/enginesession.py -> /usr/workspace/janeh/lib/matlab/engine

creating /usr/workspace/janeh/lib/matlab/_internal

copying dist/matlab/_internal/__init__.py -> /usr/workspace/janeh/lib/matlab/_internal

copying dist/matlab/_internal/mlarray_utils.py -> /usr/workspace/janeh/lib/matlab/_internal

copying dist/matlab/_internal/mlarray_sequence.py -> /usr/workspace/janeh/lib/matlab/_internal

(my-python3x6-matlab-env) janeh@flash21:~$ mv /usr/workspace/janeh/lib/matlab /usr/workspace/janeh/my-python3x6-matlab-env/lib/python3.6/site-packages/matlab

(my-python3x6-matlab-env) janeh@flash21:~$ python3 -m matlab_kernel install --prefix=$HOME/.local/

[InstallKernelSpec] Installed kernelspec matlab in /g/g0/janeh/.local/share/jupyter/kernels/matlab

Then, I updated the contents of `/g/g0/janeh/.local/share/jupyter/kernels/matlab/kernel.json` to

{"argv": ["/usr/workspace/janeh/my-python3x6-matlab-env/bin/python3”, "-m", "matlab_kernel", "-f", "{connection_file}"], "display_name": "Matlab", "language": "matlab", "mimetype": "text/x-octave", "name": "matlab"}