How to Prompt a User With a Slider in a MATLAB Live Script

MATLAB Live Scripts (.mlx files) offer a built-in interactive layer that transforms static code into dynamic, explorable documents. One of the most practical interactive controls available is the slider widget — a UI component that lets users adjust numerical values visually, without editing a single line of code. Understanding how sliders work in this environment, and what shapes their behavior, is key to building genuinely useful live documents.

What Is a Slider in a MATLAB Live Script?

A slider in a MATLAB Live Script is a graphical input control inserted directly into the document. It produces a numerical value that your code can read and respond to in real time. When a reader drags the slider handle, the associated variable updates automatically, and any downstream calculations or plots that depend on it refresh accordingly.

This is fundamentally different from a standard MATLAB script where input is handled through functions like input() — which pause execution and wait for text entry in the Command Window. In a Live Script, the slider is part of the document itself, making it ideal for educational demos, parameter exploration, and interactive reports.

How to Insert a Slider Control

MATLAB introduced interactive controls for Live Scripts in R2019b, so you'll need at least that release. Here's the core process:

  1. Open or create a .mlx file in the MATLAB Live Editor.
  2. Place your cursor in the document where you want the control to appear — typically above or near the code that uses it.
  3. In the Live Editor tab on the toolbar, click Controls, then select Slider.
  4. The slider widget appears in the document along with a paired variable name field.
  5. Set the minimum value, maximum value, step size, and default value in the control's properties panel.
  6. Assign the slider to a variable name (e.g., alpha, numPoints, threshold).

Once configured, that variable is available throughout the script. Any code cell that references it will use the current slider value when executed.

Connecting the Slider to Your Code

The slider doesn't run code automatically just by being dragged — behavior depends on how the Live Script is configured.

Run on Interaction mode causes the entire script (or a section) to re-execute whenever the slider value changes. This is the setting that produces a live, reactive feel — move the slider, watch the plot update instantly.

Manual execution means the slider sets the variable, but the user must re-run the relevant section to see the effect. This is useful when computations are expensive and you don't want them firing on every small movement.

To control this, look at the Autorun setting in the control properties. Enabling autorun on the slider triggers re-execution automatically on change.

A minimal working example looks like this: