An improved Matlab command line

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.

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) Vim key bindings, or (iii) 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.
  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: hugke729

PhD student in physical oceanography

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s