How to add a slider that prompts the user for input
MATLAB Live Files let you add interactive sliders directly into your code using the uislider function. A slider appears as a draggable control that lets someone adjust a value within a range you set, and you can write code that responds to each movement. The slider updates in real time as the user drags it, making it useful for exploring how different values affect your calculations or plots.
The basic approach is to create a figure window, define the slider's position and range, then attach a callback function that runs whenever the slider moves. In a Live File, you can place the slider and the code that uses it in the same cell, so changes happen instantly as you drag.
Key Takeaways
- Use uislider to create a slider, and set its Min, Max, and Value properties to define the range and starting point.
- Attach a callback function with the ValueChangedFcn property so your code runs each time the slider moves.
- In a Live File, define the slider and callback in the same cell so the slider stays visible while you work with its value.
- Access the current slider value with slider.Value inside your callback or anywhere else in your code.
- Position the slider using the Position property as [left, bottom, width, height] in pixel coordinates.
Creating a basic slider with a callback
Start by creating a figure and a slider object. The uislider function takes a parent figure as its first argument, then you set properties as name-value pairs. Here is a working example:
fig = uifigure; sld = uislider(fig); sld.Min = 0; sld.Max = 100; sld.Value = 50; sld.Position = [100 100 150 3];
This creates a figure window with a slider that ranges from 0 to 100, starts at 50, and sits 100 pixels from the left and bottom edges. The width is 150 pixels and the height is 3 pixels (the thickness of the slider bar itself).
To make the slider do something, add a callback function. The callback runs every time the user moves the slider. Attach it with the ValueChangedFcn property:
sld.ValueChangedFcn = @(src, event) disp(src.Value);
Now each time you drag the slider, MATLAB displays the current value in the command window. The src argument is the slider object itself, so src.Value gives you the number the user selected.
Using the slider value in calculations
A slider is most useful when its value drives a calculation or plot. Create a slider, then write code in the callback that uses the slider's value to update something on screen. Here is an example that plots a sine wave and lets the user control the frequency:
fig = uifigure; ax = uiaxes(fig); ax.Position = [100 200 400 300]; sld = uislider(fig); sld.Min = 1; sld.Max = 10; sld.Value = 1; sld.Position = [100 100 400 3];
sld.ValueChangedFcn = @(src, event) updatePlot(src.Value, ax);
function updatePlot(freq, ax) x = linspace(0, 2*pi, 1000); y = sin(freq * x); plot(ax, x, y); end
When you move the slider, the updatePlot function runs with the new slider value. The plot redraws to show a sine wave at the frequency you selected. The axes object ax is passed to the callback so the plot updates in the right place.
Positioning and sizing the slider
The Position property controls where the slider appears and how large it is. It takes a four-element vector: [left, bottom, width, height], all in pixels measured from the bottom-left corner of the figure.
For a horizontal slider (the default), set the width to control how wide it is and leave the height small (3 to 5 pixels works well). For a vertical slider, set the Orientation property to 'vertical' and swap the width and height values.
Here is a vertical slider example:
sld = uislider(fig, 'vertical'); sld.Position = [100 100 3 200];
The slider now runs vertically, 200 pixels tall and 3 pixels wide. Dragging up increases the value; dragging down decreases it.
Adding labels and tick marks
Make your slider easier to read by adding a label and tick marks. Use the Limits property to set the range (same as Min and Max), and the MajorTicks property to place tick marks at specific values:
sld.MajorTicks = [0 25 50 75 100]; sld.MajorTickLabels = {'0', '25', '50', '75', '100'};
Add a text label above or beside the slider using a uilabel object:
lbl = uilabel(fig); lbl.Text = 'Frequency'; lbl.Position = [100 130 150 20];
The label sits just above the slider and tells the user what the slider controls. You can also update the label dynamically to show the current value:
sld.ValueChangedFcn = @(src, event) updateLabel(src.Value, lbl); function updateLabel(val, lbl) lbl.Text = sprintf('Frequency: %.2f', val); end
Common issues and fixes
If your slider does not appear, check that the figure window is still open and that the Position values place the slider inside the figure bounds. A slider positioned at [0 0 100 3] sits at the very bottom-left corner; if your figure is smaller than 100 pixels wide, part of it will be cut off.
If the callback does not run, make sure you assigned it to ValueChangedFcn and not Callback (which is for buttons, not sliders). Also check that the function you reference actually exists and has the correct signature — it must accept at least two arguments, the source object and the event object.
In a Live File, if you run the cell multiple times, you may create multiple sliders. Clear the figure first with clf or close all before running the cell again, or wrap your code in a function so each run starts fresh.
Frequently Asked Questions
Can I use a slider in a regular MATLAB script, not just a Live File?
Yes, uislider works in any MATLAB code. Live Files are convenient because the slider and your code stay visible together, but you can create sliders in scripts and functions the same way. The slider will open in a separate figure window.
How do I get the slider value outside the callback function?
Store the slider object in a variable you can access later, then read slider.Value whenever you need it. If the slider is defined in one cell and you want to use it in another, make sure both cells run in the same Live File session so the variable stays in memory.
Can I limit the slider to whole numbers only?
Set the SmallStep and LargeStep properties to 1, and round the value in your callback with round(src.Value). This prevents the slider from stopping at decimal values.
What is the difference between Min/Max and Limits?
They do the same thing. Min and Max are separate properties; Limits is a two-element vector [min, max]. Use whichever is clearer in your code — sld.Min = 0; sld.Max = 100 or sld.Limits = [0 100] both work.
How do I make the slider update a variable that other parts of my code use?
Define the variable outside the callback, then update it inside the callback function. For example, freq = 1; before the slider, then sld.ValueChangedFcn = @(src, event) (freq = src.Value); inside the callback. Now freq always holds the current slider value.