Using MATLAB in Jupyter notebooks on Windows

After using R notebooks for a while I found it really unintuitive to use MATLAB in IDE. I read that it’s possible to use MATLAB with IPython but the instructions seemed a bit out of date. When I tried to follow them, I still could not run MATLAB with Jupyter (spin-off from IPython).

I wanted to conduct analyses of electroencephalographic (EEG) activity and the best plug-ins to do it (EEGLAB and ERPLAB) were written in MATLAB. I still wanted to use a programming notebook so I had to combine Jupyter and MATLAB.

I spent a bit of time setting it all up so I thought it might be worthwhile to share the process. Initially, I had three version of MATLAB (2011a, 2011b, and 2016b) and two versions of Python (2.7 and 3.3). This did not make my life easier of Windows 7.

Eventually, I only kept the installation of MATLAB 2016b to avoid problems with paths pointing to other versions. MATLAB’s Python engine works only with MATLAB 2014b or later so keeping the older versions could only cause problems.

Instructions

  • Install Anaconda (2.7)
  • Install MATLAB (>=2014b) - if you are a student then it’s very likely that your university bought a license. There is also a free MATLAB-like language called Octave, but I have not used with Jupyter. Apparently, it is possible to combine Octave with Jupyter. I’m going to focus exclusively on MATLAB in this post.
  • Install MATLAB’s Python engine - run as admin and follow the steps on the official site.
  • Once the engine was installed, I could move to installing metakernel, matlab_kernel, and pymatbridge. Go to Anaconda prompt (run as admin) and run
    pip install metakernel
  • In the Anaconda prompt run pip install matlab_kernel - this will use the development version of the MATLAB kernel.
  • Run pip install pymatbridge to install a connector between Python and MATLAB.
  • … voilà!

    MATLAB should now be available in the list of available languages.
    Once you choose it, you can start using it in a Jupyter notebook:

    Issues
    Obviously, thing were not always this smooth. Initially, I ran into problems with installing MATLAB’s Python engine. The official website suggested running the following code:
    cd "matlabroot\extern\engines\python"
    python setup.py install

    Which I did but it resulted in an error:

    Luckily, the error message was clear so I had to point Python to run the 64-bit version. I double-checked my versions with:
    import platform
    platform.architecture()

    Which returned 64-bit as expected:

    Using a command with full path to Python solved the problem:

    Summary
    I hope this will be useful. I have been messing with other issues which were pretty specific to my system so I did not include them here. Hopefully, these instructions will be enough to make MATLAB work with Jupyter.

    PS: I have also explained how to use MATLAB with Jupyter on Ubuntu.