MATLAB, Software

Matlab Live Scripts

Basics

While playing with a Matlab provided demo I discovered that it used the Live Script format as it did not look like a normal (.m) file. So I decided to look into the Matlab live scripts (.mlx) more to see if they were a viable format.

The idea is that they work like Jupiter Notebooks but with the power of Matlab toolboxes. Coming from just using Jupyter Notebooks for a project I was interested if the code and text organization could be used in Matlab the same way. I was also interested in the inline placement of Matlab graphs as they can be quite annoying when you have tons of graphs pop up in their own dedicated windows.

Matlab’s Documentation on Live Scipts

Gallery of Live Script Examples

What I Found

The live editor starts with a single section where you can place code, you can then add relevant code to that section, then when there is a logical break in your code you can create a new section using the “Section Break” button, and so on. The code sections can be run all together in sequential order or individually using the associated buttons.

Code Sections are designated by the color change shown below once selected

Now besides code you can also add plain text, images, equations, hyper links, and even control sliders to provide quick user interaction.

Now that we have the basics I decided to play around with the tools to see if it was a viable work tool. I tried some basic data processing and mathematical operations along with processing arrays and that all worked fine when there were no errors in my code or large data sets to load and work through or networked files to pull in. When there was the live editor would freeze up with almost no recourse except restarting Matlab, and when you would try to restart the Matlab the script would try to load and cause the same hang-ups. Now I understand that this is a feature of the Live Scripts in that they pre-load when you open them, but that makes iteratively working on them very difficult.

Where I can see this tool being useful is in creating final visuals for presentations, final projects, teaching tools, etc. This is because the Live Scripts have a wide variety of export options and features that allow you to create polished visuals.

Exporting Visuals

The Live Script can be exported in a variety of ways for use outside of Matlab, from PDF to Word and even HTML or LaTeX export. I took an example script and exported it to HTML so I could embed it in this post!

Live code Test

n = 50;
r = rand(n,1);
plot(r)m = mean(r);
hold on
plot([0,n],[m,m])
hold off
title('Mean of Random Uniform Data')

HTML Embedding Help

In order to post the generated html to worpress I had to remove the body paramenters as they were affecting the rest of the page, then I added content parameters to achive the correct look for the Matlab code. You can see the differece in the HTML picture below.

Left: Unmodified Right: Modified

Then I just copied the full HTML into the Gutenberg editor and changed the text sections to code view. Just copying the HTML into the custom HTML block seemed to work in preview but once published would not show the image on this blog.

Final Thoughts

I think that the Live Scripts provide more of a visual typesetting than a place to code up an algorithm and run tests. Code usually takes longer to run and minor syntax errors slow you down but on the other hand, it provides beautiful visuals for sharing graphs, code, and mathematical functions all in one place. You can also quickly change the font size with keyboard shortcuts, something that they really should migrate over to the base editor.

The major downside that hurts everybody is that the (.mlx) format is a binary… that means that there are very limited options for revision control. There should be an option for saving it in a text-based format that Matlab can operate on similar to how Jupyter Notebook (.ipynb) files are parsed.

I would see myself using it again to create an HTML visual for my blog but I will save myself some time and program a (.m) file to work out all the issues first before trying to create a Live Script.