An improved Matlab command line (plus Jupyter Notebooks for Matlab)

The command line is a large part of any Matlab user’s workflow. This vital tool, however, isn’t as user friendly as it should be: it’s cumbersome to recall multi-line commands from the history, there’s no support for Vim key bindings, and there’s no syntax highlighting if using the nodesktop option (on a remote computer, say). Fortunately, there’s an alternative that avoids these problems: IMatlab. This kernel also lets you run Matlab code in Jupyter Notebooks (originally known as IPython Notebooks).

Do I need an alternative?

Using Matlab with the approach laid out below may or may not suit your workflow. Keep reading if you want (i) a customisable, attractive command line, (ii) easy-to-use multi-line history, (iii) Vim key bindings, or (iv) a minimalist Matlab workflow.

Note also that imatlab requires Matlab R2016 or later. (matlab_kernel provides a similar functionality for older versions.)

What is IMatlab?

I’ve been spoilt in the last few years by the Jupyter console (aka the IPython console). This particularly user-friendly tool lead me on a search to find something comparable for Matlab. As it turns out, Matlab can be run as a Jupyter kernel. (You don’t need to know what a ‘Jupyter kernel’ is, by the way).

An IMatlab session is started with the command
jupyter console --kernel imatlab

For me, that session might look something like this

Unlike Matlab’s usual command line, it’s easy to scroll back through a history full of multi-line commands.

As well as being a command line tool, IMatlab can act as the kernel for a Jupyter Notebook. Let’s redo the session above, but now in a browser, where the code can be combined with documentation:

Running the IMatlab kernel in a Jupyter notebook

Using Matlab without opening Matlab

Although IMatlab is a great command line tool, it leads to the obvious question: how do I do everything else I usually do in Matlab?

  • To edit m-files, find a better editor
  • To open the help browser, type doc
  • To open a variable in the variable editor, say x , type openvar('x')
  • To show the workspace window, type workspace

Installing and customising IMatlab

Installing IMatlab isn’t too difficult, but there are a few steps. The links below go to a download or a page with the appropriate installation commands.

  1. Download and install the Anaconda distribution
    Although its a large download at 300–500MB, the Anaconda distribution greatly simplifies the installation of anything Python related. Be sure to download the Python 3, not 2, version.
    Edit: June 2018
    As of writing, the Matlab engine may not work with Python 3.6, so install with Python 3.5 instead. Also, if you receive errors during step 2 regarding CXXABI versions, see this suggestion.
  2. Install the Matlab Engine API for Python
    The instructions on this page include the command
    python install
    Ensure that the python binary being called is the one associated with the Anaconda distribution, not another installation of python somewhere else on the computer.
  3. Install IMatlab itself
    The following worked for me
    pip install imatlab --user
    python -mimatlab install --user
    See the IMatlab page for other installation options
  4. Customise
    Generate a configuration file with the command
    jupyter console --generate-config
    This will create a file in ~/.jupyter (Linux and Mac) or %PROGRAMDATA%\jupyter (Windows)
    The newly generated file is full of customisation suggestions, such as choosing Vim bindings over the default Emacs binding.

Author: Ken Hughes

Post-doctoral research scientist in physical oceanography

One thought on “An improved Matlab command line (plus Jupyter Notebooks for Matlab)”

Comments are closed.

%d bloggers like this: