Matlab-based IPython notebooks

21 February 2018, update: the new JupyterLab was just released, and according to this tweet is really easy to integrate with Matlab. Probably worth checking out instead of the reasonably outdated instructions below!

I really like Python’s philosophy, but over the last years I haven’t been able to switch the code for my research from Matlab. At this point, the transition costs are too high for me, but it’s a move I have planned for some point in the future.

Now, Python has the awesome Jupyter (formerly IPyton notebook) feature, that allows for comments, code, and most importantly graphical output (i.e. figures you’ve just generated) to be shown in one document. This is a great way to share and explain the code you’re writing, since the reader immediately sees how output is generated without having to run all the analyses themselves.

Matlab has a Publish function that attempts to do something similar, and outputs HTML files containing both code and figures. However, there are a few drawbacks: it uses its own Markup text layout language, and GitHub (where I intend to host my notebooks) does not natively render html.

Luckily, some awesome people have made it really easy to generate IPython notebooks using Matlab, that are super easy to share or host on GitHub. Here’s the step-by-step guide:

Point the kernel to your version of Matlab. I addedexport MATLAB_EXECUTABLE=/Applications/MATLAB_R2015b.app/bin/matlab to my .bash_profile file. To do this from Terminal, type echo “export MATLAB_EXECUTABLE=/Applications/MATLAB_2015b.app/bin/matlab” >> ~/.bash_profile. Of course, make sure the location and version of Matlab match yours.

Restart terminal or load .bash_profile. Type ipython notebook in Terminal. Your browser will open a Jupyter window, where on the right hand side you can go to New -> Notebooks -> Matlab.

You’re now ready to run your notebook! Simply write or copy a block of code from matlab, and click Run. The figures will automatically appear when you are calling any plotting function. When you push your Notebook.ipynb file to GitHub, it will automatically render the layout and figures. You can also download the whole notebook to HTML which can be viewed in any web browser.

See here for an example notebook, analysing some pupillometry data in Matlab. Here’s a screenshot of a notebook on signal detection theory:

This code works on a Mac OS X El Capitan, and requires some familiarity with Terminal.

Once a Jupyter notebook has been created it can be viewed by anybody through a web browser – much like an html file with figures included (eg, from the developer of the matlab_kernel, https://github.com/Calysto/matlab_kernel/blob/master/matlab_kernel.ipynb). To generate the notebooks, however, you very probably need to point the kernel to a full version of Matlab. Note that I’m not the developer of any of these tools, so you might want look up the original source code for more specifics.

That was quick 😉 Thanks.
Yeah I am aware about the portability. I was thinking to use it with the grading function of Jupyter…give students task to fulfill. And if they would need a full Matlab install on their machines that would be a major show stopper. Thanks again.

I just followed the instructions on Windows. The only difference is how to point the kernel to Matlab. In Windows you can do it under Control Panel > System. I was able to create a Matlab notebook. So far it’s writing “Connected” every few seconds. I tried to run a command but it’s taken 10 minutes now (usually it takes 30 seconds). I’m giving it some more time 🙂 If it works I’ll write a blog post on it.

Would you be willing to say a little more about what you did under Control Panel > System to get this to work? I am not sure how to go about translating the final step for pointing the kernel into windows terms.

Hi Vera,
if you go to the control panel, type “Environment” into the search bar, then the link “Edit the system environment variables” should appear. Click on it and enter username and password for an admin account (can be your user). Click on environment variables and click “New…”. Give it the name “MATLAB_EXECUTABLE” and enter the path to your matlab. This is “C:\Program Files\MATLAB\R2014a\bin\matlab.exe” in my case. (you have to use quotes!)
Does this work for you?
Best regards,
Verena

I followed the instruction, but it did not work for me. I tried to run a cell of Matlab code but nothing happens, although the current kernel was set as Matlab. Please help!! I am using a Mac OS X El Capitan.

@ Oanh Vu I had the same problem (spent 2 days trying commands in the Terminal). The tricky part is pointing your version of Matlab (you check the version at “Macintosh HD”/Applications/MATLAB_R2015a or perhaps 20XXb or something).

once you know what version of MATLAB you can automatically edit *.bash_profile*

That’s real cool, and yes, sometimes do see my self using Jupyter notebooks for some old Matlab code.
My own experience through (after failing to port some old codebase Matlab->Python) – I was able to port my Matlab libraries to Julia language, the code is clearer, faster, and supports Jupyter notebooks. I’d give it a try (the example above is easily recoded there [ using eg: https://github.com/JuliaStats/StatsFuns.jl ]