Tips & Tricks
Follow


Hans Scharler

When to use a script vs. a live script?

Hans Scharler on 18 Jan 2024
Latest activity Reply by Noah Prisament on 29 Jan 2024

Over at Reddit, a MATLAB user asked about when to use a script vs. a live script. How would you answer this?
Noah Prisament
Noah Prisament on 29 Jan 2024
Here's a few questions that may help someone come to their own conclusion:
  1. Will this code be deployed, used in production, or run frequently? If so, it may be best to use a normal script.
  2. Is this code meant to explain a process to anyone? If so, it may be best as a live script
  3. Does the code require justification or explanation? If so, it may be best as a live script
  4. Do I care about seeing/viewing the intermediary results? If so, it may be best as a live script
  5. Is my code self-documenting? If so, it is probably best as a script
  6. Do I plan on executing this script from the command window or from within other MATLAB code? Although this can also be done with a live script, it may be best to approach this with a normal script
  7. Will I be sharing this code with other people? What format do they expect to read my code in?
Sam Reinsel
Sam Reinsel on 19 Jan 2024
Similar to Mike: theres really 2 things I go straight to live scripts for, and anything else I probably just default to a classic script.
1) Early exploration/live controls
Sometimes, I've got a bunch of large data tables and I just want to selet one out of a few files, import it, plot some signals or maybe apply some filters. Try a few different plotting options!
This is super easy with live controls and live tasks. The make starting things significantly quicker if I'm not sure what I want to do yet. After I figure out what I'm doing, converting it to code (or in some cases, moving back to a normal script once im done testing things out) is pretty easy
2) Sharable, rich text file
I work with a lot of folks as a part of our Student Programs team: If i know im writing some code to try and explain a workflow, or provide some context images, equations, or tables, the rich text features make this a lot easier to do in one place. We used to do this as a word document with our code snippets and outputs added in, but you can't execute a Word document. Having the live script that can be executed to regenerate outputs (or even let people tweak the code as part of the exploration) is so much nicer for this.
But I'll add to Mikes "when i use normal scripts": If I plan to keep code in git (as more than just a demonstration file) and maintain it, i really go straight to normal scripts. But that kind of fits with his "production" code idea as well.
Hans Scharler
Hans Scharler on 19 Jan 2024
The live controls are interesting. When I add them to a live script, I am getting closer to an app. That's cool.
Mike Croucher
Mike Croucher on 19 Jan 2024
Why I use live scripts:
  • For writing the MATLAB blog. All MATLAB blog posts are written as live scripts and then converted to something WordPress can consume using this File Exchange entry Publishing tool for MATLAB® live script to WordPress - File Exchange - MATLAB Central (mathworks.com)
  • When I am exploring something or just noodling around. It feels like magic note paper that can do interactive computation. I like being able to execute code out of order while figuring out which way is up. I also find the inline graphics / output useful. Most objects (e.g. tables) are rendered better in a live script than on the command line. I like the interactivity of things like live tasks and live controls.
  • Prototyping code. Closely related to the previous point. If I am developing a new function, class or script, I try things out in the live script and steadily distill things into an .m file for production use.
  • When I need to explain something. I think of live scripts as interactive text books , interactive documentation or interactive research papers. Sure, I could write a word file or produce a pdf but better to include the code and data directly.
  • If I want to write a very simple 'app'. Using Live controls, I can take a script and turn it into a basic application with just a few clicks.
  • If I want to disemminate something. I can put the live script onto GitHub and use the 'Open in MATLAB Online' functionality to allow anyone in the world to use what I have written without needing to install MATLAB.(I talk about this in the blog last year Open science and reusable research with MATLAB Online and GitHub » The MATLAB Blog - MATLAB & Simulink (mathworks.com))
The key point that hold for all of these is the fact that live scripts are a combination of code, rich text, graphics and interactive elements.
Why I use .m files
  • To write and deliver production code.
It's the code and nothing but the code.

See Also