MaTSlab

Matlab is an archaic piece of software, is stupidly expensive, has a ridiculous syntax, horrible UI, and a whole host of bizarre quirks that you just put up with because that one grad student 10 years ago wrote a controller in it for a camera from a company which no longer exists and your supervisor refuses to buy a new one because apparently that’s a ridiculous price for only 14 bits of dynamic range.

Deep breath.

And despite all that, when I was in the zone I’m not sure I’ve ever been as effective in another piece of software. Let’s see about bringing it up to scratch.

The problem

If you haven’t used it before, Matlab is an all-in-one software package for doing numeric computation of a scientific and engineering bent that’s been around for a while – the name is a portmanteau of ‘matrix laboratory’.

Such tasteful shades of grey everywhere

I started using it during my undergraduate years. At the time the only scientific programming I’d been taught was pure C++, drawing plots with gnuplot and solving differential equations with the GNU science library. For a first-time coder that mostly cared about making pretty pictures, that approach is … inefficient.

Thus when Matlab entered my life I was immediately smitten. No compilation! No wrangling of C++ include paths! Built-in interactive plots! Suddenly I could actually do science rather than smash my head against whatever a segfault was.

On reflection there are a couple of features which kept me so efficient in Matlab:

  • Visibility of all current variables
  • Plots which are always interactive and update in real time
  • Ability to execute small chunks of code
  • Amazingly rich documentation (I really should have devoted chapter 6 of my PhD thesis to the documentation of the Matlab iradon function).

There are, however, some downsides too:

Insane syntax

Just look at this declaration of a function and tell me that it should be allowed to exist:

function ave = average(x)
    ave = sum(x(:))/numel(x); 
end

Why is the return value at the beginning? Why does index access use the same bracket as function application? Why are there no commas between elements of a tuple?

Stateful execution

In Matlab the way to execute small chunks of code is with syntax like the following, where blocks are separated by %%

%%
a = 1 % Yes this is the comment character. I know. Yes, I know.
%%
a = 2
%%

Executing the first block will set the variable a to 1, and the second to 2. Every block has access to all other declared variables at any time, so it’s very easy to accidentally overwrite something, or write your code in a way that means blocks have to be executed in a specific order. (Jupyter et al. also share this problem.)

Difficult to share code

Running Matlab code outside of the Matlab IDE is a massive pain. You can bundle it up with a runtime (which worked… most of the time), but the runtime comes in at hundreds of megabytes. There is an open-source implementation of Matlab (called Octave), but it doesn’t support all of the latest features.

The solution

I put up with these failings for several years before eventually shifting most of my work to python and C++, but I’ve never quite filled that Matlab-shaped hole in my life.

Fortunately, since then I’ve learned just enough web development to be dangerous, so let’s see how far we can get by bashing some stuff together that other people wrote. I present:

MaTSlab

Gotta love a sinusoid for making something look sciencey.

The TS stands for Typescript – the language I wrote this project in, and the language you use in MaTSlab. Typescript is a great language for a couple of main reasons:

  • Unlike Javascript it is typed, enabling autocompletion, compilation errors and all that goodness
  • You can transpile it to Javascript, which runs in the browser without any extra work
  • There’s some really great open-source tooling built around it that I could leverage

You can see in the screenshot above that I took the 3 features of Matlab that I liked, namely variables, cells, and plots, and dumped the things I didn’t.

Let’s look at the benefits of the points above.

Types

I mean, the clue’s in the name, but Typescript infers a type for everything that you write (in most cases). This means it will pick up tons of silly errors:

Isn’t it beautiful
This is fun

Running in the browser

It doesn’t need to be said that modern browsers are swiss army knives, making use of almost every tool that computer science has come up with.

In this case, the fact that every browser ships with an integrated parser, compiler, and heavily optimised runtime for Javascript means that running code can be pretty quick. It’ll never be as fast as native compiled code of course, but it can come close enough that the difference often doesn’t matter.

You are also able to make use of modern APIs for interacting with web resources, for example: grabbing the latest coronovirus case numbers for the UK.

Coronovirus example

or using, ahem, rich content:

Embed example

It’s one second longer than the 10 minute ad limit, the cheeky bastards. Don’t they earn enough from their insane pricing for the image processing toolbox?

Stealing Using other people’s code

This is one of the biggest advantages of using a popular language – lots of pre-existing code that can be leveraged to sped you up. Matlab does have some code sharing on the File Exchange, and I may have even submitted a 5-star example in my time, but it pales in comparison to many other languages.

For example, here I’ve literally copy-pasted in

  • All of the Typescript transpilation logic – the following code isn’t Javascript, but the in-browser compiler transforms it into code that the browser can run:
  • A fully-featured code editor with syntax highlighting and integration with a Typescript type checker running in another process (in the browser!)
Type checker demo
  • All of the React rendering library for displaying arbitrary HTML content
  • The Plotly visualisation library, with GPU-accelerated visuals

My only contribution is mashing them all together, into a frankly massive and messy ball of Javascript. I’ve also added a couple of convenience functions like linspace, but there are very few of those.

Why you should use MaTSlab over Matlab

  • Scoped variables – only share values between cells that you care about by explicitly ‘exporting’ them. Note in the following that notPi is not visible outside of cell a, so you can’t accidentally use it.
  • A sane(r) language – Javascript isn’t the sanest language by a long way, but getting Typescript involves helps a huge amount, and it certainly beats the hell out of whatever Matlab is going for. You want to wrap everything in a class because that’s what your 80-year-old CS teacher recommended? Go for it!
OOP example
  • It works natively with the web – obviously it’s already running in the browser, but that also means it is trivial to integrate with other web resources. In the example below, harkening back to an older post of mine, you can see the rainfall and local river level near my eminently floodable house.
  • It doesn’t take 2 hours to install – this is not an exaggeration. When I used to do science experiments, we’d sometimes use our university copy of Matlab on machines at other labs, which meant installing a fresh copy. This process involved many gigabytes of downloads, and literal hours of watching a progress bar crawl across the screen while wishing you were dead (that last part is probably not the fault of Matlab). While, obviously, this demo doesn’t do everything Matlab does, it’s small enough that I can stick it on GitHub and serve it from there.
  • Sharing is easy – the URL is updated as you type, containing a compressed representation of your code and plots. Just share the URL, like I’ve done in this post.

Now I’ve scratched my itch by throwing this demo online, time for you to scratch yours. Let me know what you make!

Examples

MaTSlab

Coronovirus example

Embed example

Transpilation demo

Type checker demo

React demo

Plot demo

Scoping example

OOP example

Water level demo

2 thoughts on “MaTSlab

  1. This is awesome Jason. You might also be interested in https://observablehq.com
    It removes all state, and is one of the better notebook experiences imo. Colab also can be finagled into running javascript: https://colab.to/js
    But I don’t really know of any other TypeScript notebooks, so that’s neat.

    Also, you might run into trouble with the bae64 encoded urls- if they get too big, GET requests will start to fail. But yeah, anything over inheriting Matlab code (except maybe Fortran)

    Like

Leave a Reply

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

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s