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.
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.
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’.
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
There are, however, some downsides too:
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?
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.
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:
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:
- 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.
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:
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.
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.
or using, ahem, rich content:
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
- A fully-featured code editor with syntax highlighting and integration with a Typescript type checker running in another process (in the browser!)
- All of the React rendering library for displaying arbitrary HTML content
- The Plotly visualisation library, with GPU-accelerated visuals
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
notPiis not visible outside of cell
a, so you can’t accidentally use it.
- 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!