Setup#

I have used Jupyter notebooks for EG-150 Signals and Systems for a number of reasons:

  1. I can easily produce maths-rich textbook quality notes using the Markdown system provided for documentation blocks.

  2. I can generate a slide-show from my notes but also print them as PDF files for your convenience.

  3. I can interweave live coding examples with my notes and execute and change these examples in a live classroom.

  4. … more interestingly, you can take the notebooks and experiment with the computing examples yourself!

However, to fully access all the examples that have been provided as Jupyter notebooks for EG-150 you will need to install both MATLAB (I used MATLAB 2022b) and Python 3 (I used Anaconda 3) and this is something of a technical challenge.

The installation of Anaconda 3 (which includes Jupyter Notebook) and MATLAB is described elsewhere but must be done before you can open and execute this notebook.

Assuming that you have installed Anaconda 3, you can download and open this notebook using the command:

jupyter notebook setup.ibynb

Alternatively, you can use the Anaconda Navigator to launch a Jupyter Notebook and then navigate to the setup.ipynb file.

Note for Windows users, you need to start jupyter as an Administrator.

Advanced users#

You may prefer to use the python environments and the command line, in which case, refer to Advanced Settings.

About this notebook#

A Jupyter notebook is a combination of documentation and code cells. It is both a sequence of commands (in this case written in Python) that can be executed in sequence and a record of that execution. This notebook documents the process of installing the MATLAB kernel for Jupyter.

You should be able to run each code cell in turn without errors. To execute code in a Jupyter notebook, select a code cell and press the run button or type Shift-Enter. The next cell will automatically be selected.

Alternatively you can simply run the whole notebook by selecting the Cell->Run All command from the menu.

Set up anaconda environment#

Test Base Setup#

The following Python code (adapted from the script soton-test-python-installation.py [1]) can be executed to report whether particular python packages are available on the system.

First we define some tests.

import math
import os
import sys


def test_is_python_35():
    major = sys.version_info.major
    minor = sys.version_info.minor
    if major == 3:
        pass 
    else:
        print("You are running Python {}, but we need Python {}.".format(major, 3))
        print("Download and install the Anaconda distribution for Python 3.")
        print("Stopping here.")

        # Let's stop here
        sys.exit(1)
        return None
        # assert major == 3, "Stopping here - we need Python 3."

    if minor >= 5:
        print("Testing Python version-> py{}.{} OK".format(major, minor))
    else:
        print("Warning: You should be running Python 3.5 or newer, " +
              "you have Python {}.{}.".format(major, minor))
        
        
def test_numpy():
    try:
        import numpy as np
    except ImportError:
        print("Could not import numpy -> numpy failed")
        return None
    # Simple test
    a = np.arange(0, 100, 1)
    assert np.sum(a) == sum(a)
    print("Testing numpy...      -> numpy OK")


def test_scipy():
    try:
        import scipy
    except ImportError:
        print("Could not import 'scipy' -> scipy failed")
        return None
    # Simple test
    import scipy.integrate
    assert abs(scipy.integrate.quad(lambda x: x * x, 0, 6)[0] - 72.0) < 1e-6
    print("Testing scipy ...     -> scipy OK")


def test_pylab():
    """Actually testing matplotlib, as pylab is part of matplotlib."""
    try:
        import pylab
    except ImportError:
            print("Could not import 'matplotlib/pylab' -> failed")
            return None
    # Creata plot for testing purposes
    xvalues = [i * 0.1 for i in range(100)]
    yvalues = [math.sin(x) for x in xvalues]
    pylab.plot(xvalues, yvalues, "-o", label="sin(x)")
    pylab.legend()
    pylab.xlabel('x')
    testfilename='pylab-testfigure.png'

    # check that file does not exist yet:
    if os.path.exists(testfilename):
        print("Skipping plotting to file as file {} exists already."\
            .format(testfilename))
    else:
        # Write plot to file
        pylab.savefig(testfilename)
        # Then check that file exists
        assert os.path.exists(testfilename)
        print("Testing matplotlib... -> pylab OK")
        os.remove(testfilename)


def test_sympy():
    try:
        import sympy
    except ImportError:
            print("Could not import 'sympy' -> fail")
            return None
    # simple test
    x = sympy.Symbol('x')
    my_f = x ** 2
    assert sympy.diff(my_f,x) == 2 * x
    print("Testing sympy         -> sympy OK")


def test_pytest():
    try:
        import pytest
    except ImportError:
            print("Could not import 'pytest' -> fail")
            return None
    print("Testing pytest        -> pytest OK")
Error: Invalid expression. Check for missing or extra characters.


The we run the tests to test that we have all the packages we need. If we have installed Anaconda 3 correctly, there should be no errors.

print("Running using Python {}".format(sys.version))
test_is_python_35()
test_numpy()
test_scipy()
test_pylab()
test_sympy()
test_pytest()
Invalid use of operator.


The remaining installation instructions are adapted from [2].

Python-MATLAB Bridge#

To install this, we first have to install MATLAB. I’m assuming that this has been done and you have MATLAB 2017b or greater installed.

Now we install the Python-MATLAB bridge.

Here we’ve adapted the instructions given in the official MATLAB documentation MATLAB API for Python.

I ran this on my Mac. The equivalent Windows and Linux commands are given in the comments.

Mac OS:#

matlabroot='/Applications/MATLAB_R2023b.app'
matlabroot =

    '/Applications/MATLAB_R2023b.app'

Unix#

%cd {matlabroot}/extern/engines/python
[Errno 2] No such file or directory: '{matlabroot}/extern/engines/python'

Ubuntu running in Windows using WSL#

matlabroot='/mnt/c/Program\ Files/MATLAB/R2023a'
%cd {matlabroot}/extern/engines/python
matlabroot =

    '/mnt/c/Program\ Files/MATLAB/R2023a'

Windows#

matlabroot='C:\Program Files\MATLAB\R202a'
%cd {matlabroot}\extern\engines\python
matlabroot =

    'C:\Program Files\MATLAB\R202a'

Install MATLAB Engine#

Notes

  • On windows you have to do this step as an admin user

    • Open the anaconda prompt as administrator.

    • Copy result of previous command

    • type cd then paste what you’ve just copied and type Enter

    • Now copy python setup.py install, paste and type Enter

  • If your MATLAB is 2016b, or older, you may need to install an earlier version of Python and repeat the steps above.

!python --version
#%shell
!python -m pip install . 
Python 3.11.6
Error: Invalid text character. Check for unsupported symbol, invisible character, or pasting of non-ASCII characters.


Test Python can now communicate with MATLAB#

First start a MATLAB session. You will have to restart your Python kernel first!

import matlab.engine
eng = matlab.engine.start_matlab()
Error: Unable to find or import 'matlab.engine'. Imported names must end with '.*' or be fully qualified.


Then connect to the session

eng = matlab.engine.connect_matlab()
Unable to resolve the name 'matlab.engine.connect_matlab'.


Now compute something. Here’s a 10x10 magic square

m = eng.magic(10);
Unable to resolve the name 'eng.magic'.


print(m)
Error using eval
Unrecognized function or variable 'm'.


Close the session

eng.quit()
Unable to resolve the name 'eng.quit'.


MATLAB Kernel for Jupyter#

Finally we install the matlab_kernel using the instructions given here: gthub.com/mathworks/jupyter-matlab-proxy.

!python3 -m pip install jupyter-matlab-proxy
Requirement already satisfied: jupyter-matlab-proxy in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (0.10.0)

Requirement already satisfied: jupyter-contrib-nbextensions in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-matlab-proxy) (0.7.0)

Requirement already satisfied: jupyter-server-proxy in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-matlab-proxy) (4.1.0)

Requirement already satisfied: matlab-proxy>=0.10.0 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-matlab-proxy) (0.10.0)

Requirement already satisfied: psutil in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-matlab-proxy) (5.9.0)

Requirement already satisfied: requests in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-matlab-proxy) (2.31.0)

Requirement already satisfied: simpervisor>=1.0.0 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-matlab-proxy) (1.0.0)

Requirement already satisfied: aiohttp>=3.7.4 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from matlab-proxy>=0.10.0->jupyter-matlab-proxy) (3.8.5)

Requirement already satisfied: aiohttp-session[secure] in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from matlab-proxy>=0.10.0->jupyter-matlab-proxy) (2.12.0)

Requirement already satisfied: ipython-genutils in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-contrib-nbextensions->jupyter-matlab-proxy) (0.2.0)

Requirement already satisfied: jupyter-contrib-core>=0.3.3 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-contrib-nbextensions->jupyter-matlab-proxy) (0.4.2)

Requirement already satisfied: jupyter-core in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-contrib-nbextensions->jupyter-matlab-proxy) (5.5.0)

Requirement already satisfied: jupyter-highlight-selected-word>=0.1.1 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-contrib-nbextensions->jupyter-matlab-proxy) (0.2.0)

Requirement already satisfied: jupyter-nbextensions-configurator>=0.4.0 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-contrib-nbextensions->jupyter-matlab-proxy) (0.6.3)

Requirement already satisfied: nbconvert>=6.0 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-contrib-nbextensions->jupyter-matlab-proxy) (7.10.0)

Requirement already satisfied: notebook>=6.0 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-contrib-nbextensions->jupyter-matlab-proxy) (6.5.6)

Requirement already satisfied: tornado in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-contrib-nbextensions->jupyter-matlab-proxy) (6.3.3)

Requirement already satisfied: traitlets>=4.1 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-contrib-nbextensions->jupyter-matlab-proxy) (5.7.1)

Requirement already satisfied: lxml in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-contrib-nbextensions->jupyter-matlab-proxy) (4.9.3)

Requirement already satisfied: jupyter-server>=1.0 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-server-proxy->jupyter-matlab-proxy) (1.24.0)

Requirement already satisfied: charset-normalizer<4,>=2 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from requests->jupyter-matlab-proxy) (2.0.4)

Requirement already satisfied: idna<4,>=2.5 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from requests->jupyter-matlab-proxy) (3.4)

Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from requests->jupyter-matlab-proxy) (1.26.18)

Requirement already satisfied: certifi>=2017.4.17 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from requests->jupyter-matlab-proxy) (2023.11.17)

Requirement already satisfied: attrs>=17.3.0 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from aiohttp>=3.7.4->matlab-proxy>=0.10.0->jupyter-matlab-proxy) (23.1.0)

Requirement already satisfied: multidict<7.0,>=4.5 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from aiohttp>=3.7.4->matlab-proxy>=0.10.0->jupyter-matlab-proxy) (6.0.2)

Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from aiohttp>=3.7.4->matlab-proxy>=0.10.0->jupyter-matlab-proxy) (4.0.2)

Requirement already satisfied: yarl<2.0,>=1.0 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from aiohttp>=3.7.4->matlab-proxy>=0.10.0->jupyter-matlab-proxy) (1.8.1)

Requirement already satisfied: frozenlist>=1.1.1 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from aiohttp>=3.7.4->matlab-proxy>=0.10.0->jupyter-matlab-proxy) (1.4.0)

Requirement already satisfied: aiosignal>=1.1.2 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from aiohttp>=3.7.4->matlab-proxy>=0.10.0->jupyter-matlab-proxy) (1.2.0)

Requirement already satisfied: setuptools in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-contrib-core>=0.3.3->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (68.0.0)

Requirement already satisfied: pyyaml in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-nbextensions-configurator>=0.4.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (6.0.1)

Requirement already satisfied: anyio<4,>=3.1.0 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-server>=1.0->jupyter-server-proxy->jupyter-matlab-proxy) (3.5.0)

Requirement already satisfied: argon2-cffi in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-server>=1.0->jupyter-server-proxy->jupyter-matlab-proxy) (21.3.0)

Requirement already satisfied: jinja2 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-server>=1.0->jupyter-server-proxy->jupyter-matlab-proxy) (3.1.2)

Requirement already satisfied: jupyter-client>=6.1.12 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-server>=1.0->jupyter-server-proxy->jupyter-matlab-proxy) (7.4.9)

Requirement already satisfied: nbformat>=5.2.0 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-server>=1.0->jupyter-server-proxy->jupyter-matlab-proxy) (5.9.2)

Requirement already satisfied: packaging in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-server>=1.0->jupyter-server-proxy->jupyter-matlab-proxy) (23.1)

Requirement already satisfied: prometheus-client in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-server>=1.0->jupyter-server-proxy->jupyter-matlab-proxy) (0.14.1)

Requirement already satisfied: pyzmq>=17 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-server>=1.0->jupyter-server-proxy->jupyter-matlab-proxy) (24.0.1)

Requirement already satisfied: Send2Trash in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-server>=1.0->jupyter-server-proxy->jupyter-matlab-proxy) (1.8.2)

Requirement already satisfied: terminado>=0.8.3 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-server>=1.0->jupyter-server-proxy->jupyter-matlab-proxy) (0.17.1)

Requirement already satisfied: websocket-client in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-server>=1.0->jupyter-server-proxy->jupyter-matlab-proxy) (0.58.0)

Requirement already satisfied: platformdirs>=2.5 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-core->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (3.10.0)

Requirement already satisfied: beautifulsoup4 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from nbconvert>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (4.12.2)

Requirement already satisfied: bleach!=5.0.0 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from nbconvert>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (4.1.0)

Requirement already satisfied: defusedxml in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from nbconvert>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (0.7.1)

Requirement already satisfied: jupyterlab-pygments in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from nbconvert>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (0.1.2)

Requirement already satisfied: markupsafe>=2.0 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from nbconvert>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (2.1.1)

Requirement already satisfied: mistune<4,>=2.0.3 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from nbconvert>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (2.0.4)

Requirement already satisfied: nbclient>=0.5.0 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from nbconvert>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (0.7.4)

Requirement already satisfied: pandocfilters>=1.4.1 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from nbconvert>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (1.5.0)

Requirement already satisfied: pygments>=2.4.1 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from nbconvert>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (2.15.1)

Requirement already satisfied: tinycss2 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from nbconvert>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (1.2.1)

Requirement already satisfied: nest-asyncio>=1.5 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from notebook>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (1.5.6)

Requirement already satisfied: ipykernel in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from notebook>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (6.25.0)

Requirement already satisfied: nbclassic>=0.4.7 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from notebook>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (1.0.0)

Requirement already satisfied: cryptography in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from aiohttp-session[secure]->matlab-proxy>=0.10.0->jupyter-matlab-proxy) (41.0.3)

Requirement already satisfied: sniffio>=1.1 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from anyio<4,>=3.1.0->jupyter-server>=1.0->jupyter-server-proxy->jupyter-matlab-proxy) (1.2.0)

Requirement already satisfied: six>=1.9.0 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from bleach!=5.0.0->nbconvert>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (1.16.0)

Requirement already satisfied: webencodings in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from bleach!=5.0.0->nbconvert>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (0.5.1)

Requirement already satisfied: entrypoints in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-client>=6.1.12->jupyter-server>=1.0->jupyter-server-proxy->jupyter-matlab-proxy) (0.4)

Requirement already satisfied: python-dateutil>=2.8.2 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jupyter-client>=6.1.12->jupyter-server>=1.0->jupyter-server-proxy->jupyter-matlab-proxy) (2.8.2)

Requirement already satisfied: notebook-shim>=0.2.3 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from nbclassic>=0.4.7->notebook>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (0.2.3)

Requirement already satisfied: fastjsonschema in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from nbformat>=5.2.0->jupyter-server>=1.0->jupyter-server-proxy->jupyter-matlab-proxy) (2.16.2)

Requirement already satisfied: jsonschema>=2.6 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from nbformat>=5.2.0->jupyter-server>=1.0->jupyter-server-proxy->jupyter-matlab-proxy) (4.19.2)

Requirement already satisfied: ptyprocess in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from terminado>=0.8.3->jupyter-server>=1.0->jupyter-server-proxy->jupyter-matlab-proxy) (0.7.0)

Requirement already satisfied: argon2-cffi-bindings in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from argon2-cffi->jupyter-server>=1.0->jupyter-server-proxy->jupyter-matlab-proxy) (21.2.0)

Requirement already satisfied: soupsieve>1.2 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from beautifulsoup4->nbconvert>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (2.5)

Requirement already satisfied: cffi>=1.12 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from cryptography->aiohttp-session[secure]->matlab-proxy>=0.10.0->jupyter-matlab-proxy) (1.16.0)

Requirement already satisfied: appnope in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from ipykernel->notebook>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (0.1.2)

Requirement already satisfied: comm>=0.1.1 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from ipykernel->notebook>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (0.1.2)

Requirement already satisfied: debugpy>=1.6.5 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from ipykernel->notebook>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (1.6.7)

Requirement already satisfied: ipython>=7.23.1 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from ipykernel->notebook>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (8.15.0)

Requirement already satisfied: matplotlib-inline>=0.1 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from ipykernel->notebook>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (0.1.6)

Requirement already satisfied: pycparser in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from cffi>=1.12->cryptography->aiohttp-session[secure]->matlab-proxy>=0.10.0->jupyter-matlab-proxy) (2.21)

Requirement already satisfied: backcall in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from ipython>=7.23.1->ipykernel->notebook>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (0.2.0)

Requirement already satisfied: decorator in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from ipython>=7.23.1->ipykernel->notebook>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (5.1.1)

Requirement already satisfied: jedi>=0.16 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from ipython>=7.23.1->ipykernel->notebook>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (0.18.1)

Requirement already satisfied: pickleshare in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from ipython>=7.23.1->ipykernel->notebook>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (0.7.5)

Requirement already satisfied: prompt-toolkit!=3.0.37,<3.1.0,>=3.0.30 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from ipython>=7.23.1->ipykernel->notebook>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (3.0.36)

Requirement already satisfied: stack-data in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from ipython>=7.23.1->ipykernel->notebook>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (0.2.0)

Requirement already satisfied: pexpect>4.3 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from ipython>=7.23.1->ipykernel->notebook>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (4.8.0)

Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jsonschema>=2.6->nbformat>=5.2.0->jupyter-server>=1.0->jupyter-server-proxy->jupyter-matlab-proxy) (2023.7.1)

Requirement already satisfied: referencing>=0.28.4 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jsonschema>=2.6->nbformat>=5.2.0->jupyter-server>=1.0->jupyter-server-proxy->jupyter-matlab-proxy) (0.30.2)

Requirement already satisfied: rpds-py>=0.7.1 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jsonschema>=2.6->nbformat>=5.2.0->jupyter-server>=1.0->jupyter-server-proxy->jupyter-matlab-proxy) (0.10.6)

Requirement already satisfied: parso<0.9.0,>=0.8.0 in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from jedi>=0.16->ipython>=7.23.1->ipykernel->notebook>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (0.8.3)

Requirement already satisfied: wcwidth in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from prompt-toolkit!=3.0.37,<3.1.0,>=3.0.30->ipython>=7.23.1->ipykernel->notebook>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (0.2.5)

Requirement already satisfied: executing in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from stack-data->ipython>=7.23.1->ipykernel->notebook>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (0.8.3)

Requirement already satisfied: asttokens in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from stack-data->ipython>=7.23.1->ipykernel->notebook>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (2.0.5)

Requirement already satisfied: pure-eval in /Users/eechris/anaconda3/envs/eg-150-textbook/lib/python3.11/site-packages (from stack-data->ipython>=7.23.1->ipykernel->notebook>=6.0->jupyter-contrib-nbextensions->jupyter-matlab-proxy) (0.2.2)

!python3 -m pip install 'jupyterlab>=3.0.0,<4.0.0a0'
bash: 4.0.0a0: No such file or directory

To check that the MATLAB kernel is properly installed do the following.

  1. Save this notebook File->Save.

  2. Select Kernel->Shutdown from the notebook menu.

  3. Close the browser window.

  4. Restart the Jupiter Notebook, as you did at the start, then return here:

jupyter notebook setup.ipynb --debug

Test MATLAB Kernel#

From the Kernel menu you should be able to navigate to Change kernel and MATLAB should now be listed (Fig. 1).

Figure 1: The Kernel Menu

Figure 1: The Kernel Menu

Go ahead and switch to the MATLAB kernel.

If all is well, you should see the Kernel indicator (top right) change to ‘MATLAB’ (Fig. 2)

Figure 2: MATLAB kernel indicator

Figure 2: The MATLAB kernel indicator

You should now be able to execute the MATLAB magic(10) function again and get the result shown:

magic(10)

ans =

    92    99     1     8    15    67    74    51    58    40
    98    80     7    14    16    73    55    57    64    41
     4    81    88    20    22    54    56    63    70    47
    85    87    19    21     3    60    62    69    71    28
    86    93    25     2     9    61    68    75    52    34
    17    24    76    83    90    42    49    26    33    65
    23     5    82    89    91    48    30    32    39    66
    79     6    13    95    97    29    31    38    45    72
    10    12    94    96    78    35    37    44    46    53
    11    18   100    77    84    36    43    50    27    59

Go ahead and execute the next code cell.

magic(10)
ans =

    92    99     1     8    15    67    74    51    58    40
    98    80     7    14    16    73    55    57    64    41
     4    81    88    20    22    54    56    63    70    47
    85    87    19    21     3    60    62    69    71    28
    86    93    25     2     9    61    68    75    52    34
    17    24    76    83    90    42    49    26    33    65
    23     5    82    89    91    48    30    32    39    66
    79     6    13    95    97    29    31    38    45    72
    10    12    94    96    78    35    37    44    46    53
    11    18   100    77    84    36    43    50    27    59

If you wish to further test the MATLAB interface, download this file from the Calysto/matlab_kernel repository matlab_kernel.ipynb, open it in Jupyter and run the whole notebook.

If you have any problems, send me a message through the Teams page for the EG-247 Course.

For More Information on Jupyter#

To learn more about Jupyter notebooks, the key resource is the Jupyter Project site itself. There, under the documentation section, you will find everything you need to fully understand Jupyter. However, it’s not arranged in a way that is useful for a beginner!

For a quick introduction, I particularly recommend Corey Schafer’s YouTube tutorial: https://youtu.be/HW29067qVWk.

%%html
<iframe width="560" height="315" src="https://www.youtube.com/embed/HW29067qVWk" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

Jupyter Noteboooks versus MATLAB Live Scripts#

Both Jupyter and MATLAB Live Script trace their origins – or at least their inspiration – to the Mathematica Notebook interface. All allow the mixing of code with output, the running of code in sections, and the ability to add formatted text, images, and equations to tell a story and provide a repeatable record of a computation.

The Mathworks claims some advantages for the MATLAB Live Script interface due to its close integration with the MATLAB desktop and the new MATLAB Online product. Other teachers have also advocated the use of Live Script in teaching, e.g. Teaching with MATLAB Live Scripts. The main issue though is that MATLAB is an expensive, licensed product. It’s free to use while you are a student or a teacher at Swansea University. It is extremely expensive once you graduate!

For me, the main advantage of Jupyter notebooks is that it is language independent, well supported, open source and free! It also has some features, mentioned at the top of this document, that make it particularly attractive as a support tool for the teaching on this course.

That said, we will be using MATLAB throughout this course and MATLAB Live Scripts in the Labs for this module.

References#

  1. Fangohr, Prof Hans, Installation of Python, Spyder, Numpy, Sympy, Scipy, Pytest, Matplotlib via Anaconda, University of Southampton, 20116. Available from: https://fangohr.github.io/blog/installation-of-python-spyder-numpy-sympy-scipy-pytest-matplotlib-via-anaconda.html.

  2. Blank, Doug, Silvester, Steven and Lee, Antony, Calysto/matlab_kernel README, Calysto, 2017. Available from GitHub: Calysto/matlab_kernel.

  3. Lee, Antony and collaborators, A Jupyter kernel for MATLAB, 2021. Available from GitHub: imatlab/imatlab.