How to add a slider prompt to a MATLAB Live file

A slider in a MATLAB Live Script lets you create an interactive control that a reader can drag to change a value, without writing any code themselves. You add it using the uislider function inside a Live Script cell. The slider appears as a horizontal bar with a handle; when someone moves it, the value updates when ready and can trigger calculations or plots below.

The simplest approach is to create a slider in one cell, then reference its value in cells below. MATLAB Live Scripts treat each cell somewhat independently, so you need to make sure the slider variable stays in memory as you work through subsequent cells.

Key Takeaways

  • Use uislider inside a Live Script cell to create an interactive slider that updates a variable when moved.
  • Set the slider's Limits property to define the minimum and maximum values, and Value to set where it starts.
  • Attach a callback function with ValueChangedFcn to run code automatically whenever someone moves the slider.
  • Reference the slider's current value in other cells using the variable name you assigned to it, such as slider.Value.
  • Live Scripts require the slider to be created in the same session; rerunning the entire script will reset the slider position.

Creating a basic slider in a Live Script cell

Start by opening a new Live Script (File > New > Live Script, or use the .mlx file format). In the first cell, create a figure and then add the slider to it. Here is the actual code:

fig = uifigure; sld = uislider(fig); sld.Limits = [0 100]; sld.Value = 50;

This creates a window with a slider that ranges from 0 to 100 and starts at 50. The variable sld now holds the slider object, and you can read its current position anytime by checking sld.Value. If you want the slider to appear inside the Live Script output area rather than in a separate window, use uifigure as shown — it embeds the interface into the script.

Setting slider properties to match your needs

Beyond Limits and Value, you can customize how the slider behaves. The MajorTicks property adds visible tick marks; MajorTickLabels labels them. The Step property controls how much the value changes when someone clicks the slider track (not the handle itself).

For example, if you want a slider that goes from 1 to 10 in steps of 1, with labels at each tick:

sld.Limits = [1 10]; sld.MajorTicks = [1 2 3 4 5 6 7 8 9 10]; sld.MajorTickLabels = {'1','2','3','4','5','6','7','8','9','10'}; sld.Step = 1;

You can also add a label above the slider using the uilabel function in the same figure, so the reader knows what the slider controls. This makes the interface much clearer, especially if you have multiple sliders.

Running code automatically when the slider moves

To make the slider actually do something, attach a callback function using ValueChangedFcn. This tells MATLAB to run specific code whenever someone moves the slider. In a Live Script, you typically define the callback as an anonymous function or a nested function.

Here is a complete example that updates a plot as you move the slider:

fig = uifigure; ax = uiaxes(fig); sld = uislider(fig); sld.Limits = [0.1 5]; sld.Value = 1; sld.ValueChangedFcn = @(src,event) updatePlot(ax, src.Value); function updatePlot(ax, frequency) x = linspace(0, 2*pi, 100); y = sin(frequency * x); plot(ax, x, y); end

When you move the slider, the callback passes the new value to the updatePlot function, which recalculates and redraws the sine wave. The frequency changes based on where the slider is positioned. This pattern works for any calculation or visualization you want to update interactively.

Referencing slider values in other cells

Once you create the slider in one cell, you can use its value in cells below by referencing the variable you assigned to it. If your first cell creates sld, then in a later cell you can write:

currentValue = sld.Value; result = currentValue * 2; disp(result);

Each time you run this cell, it reads whatever value the slider is currently set to. This is useful for building multi-step workflows where the slider controls a parameter and later cells perform calculations based on it. Keep in mind that if you close the figure or restart MATLAB, the slider disappears and the variable becomes invalid.

Common issues and how to fix them

One frequent problem is that the slider appears in a separate window instead of embedded in the Live Script. This happens if you use figure instead of uifigure. Always use uifigure in a Live Script to keep the interface visible in the output area.

Another issue is that the callback function does not run. Make sure you have assigned it correctly using ValueChangedFcn = @(src,event) functionName(...). The src parameter refers to the slider itself, so src.Value gives you the current position. If the function is defined in the same cell, it must be a nested function (defined at the end of the cell after the main code).

If you run the entire script and the slider resets to its starting position, that is normal behavior — Live Scripts re-execute all cells from top to bottom, so the slider is recreated each time. To preserve slider state across runs, you would need to save it to a file or use persistent variables, which is beyond the scope of basic interactive sliders.

Frequently Asked Questions

Can I have multiple sliders in one Live Script?

Yes. Create each slider in the same figure and assign it to a different variable, such as sld1, sld2, and sld3. Position them vertically by adjusting their Position property, or create separate figures for each. Each slider can have its own callback function.

How do I make the slider show decimal values instead of just integers?

Set the Step property to a decimal, such as sld.Step = 0.1;. The slider will then move in increments of 0.1. You can also format the display using MajorTickLabels to show only certain values, even if the slider itself moves smoothly.

What is the difference between uislider and uicontrol slider?

uislider is the newer function and works better in Live Scripts and modern MATLAB apps. uicontrol with style='slider' is older and less flexible. Use uislider for new code.

Can I use a slider to control multiple things at once?

Yes. In the callback function, run as much code as you need. For example, the callback can update a plot, recalculate a table, and display a text value all at the same time. Just put all the operations inside the callback function.

Does the slider work if I share the Live Script with someone else?

The slider will appear when they open the file, but they will need to run the cell that creates it first. Once they run that cell, they can move the slider and run other cells that depend on it. The slider is interactive only while the Live Script is open in MATLAB.