Create slider component
Use only with App Designer or figures
created with the uifigure
function. When
using GUIDE or the figure
function, create a
slider using uicontrol
.
creates
a slider in a new UI figure window and returns the slider object.sld
= uislider
specifies
the parent object in which to create the slider. The parent object
must be a UI figure window, or a tab, panel, or button group within
a UI figure window. sld
= uislider(parent
)
specifies
slider properties using one or more sld
= uislider(___,Name,Value
)Name,Value
pair
arguments. Use this option with any of the input argument combinations
in the previous syntaxes.
parent
— Parent object of sliderParent object of the slider, specified as a figure, panel, tab or button group object.
The parent object must be either a figure created using the uifigure
function,
or a tab, panel, or button group within such a figure.
Specify optional comma-separated pairs of Name,Value
arguments.
Name
is the argument
name and Value
is the corresponding
value. Name
must appear
inside single quotes (' '
).
You can specify several name and value pair
arguments in any order as Name1,Value1,...,NameN,ValueN
.
'Limits',[0 50]
specifies the minimum
slider value as 0
and the maximum slider value
as 50
.The properties listed here are a subset of the available properties. For the full list, see Slider (App Designer) Properties.
'Value'
— Slider valueSlider value, specified as a numeric value. The numeric value
must be within the range specified by the Limits
property
value.
'Limits'
— Minimum and maximum slider valuesMinimum and maximum slider values, specified as a two-element numeric array. The first value must be less than the second value.
If you change Limits
such that Value
is
less than the new lower limit, MATLAB® sets Value
to
the new lower limit. For example, suppose Limits
is [0
100]
and Value
is 20. If the Limits
changes
to [50 100]
, then MATLAB sets the Value
to
50.
Similarly, if you change Limits
such that
the slider Value
is greater than the new upper
limit, MATLAB sets the Value
to the new
upper limit.
'MajorTicks'
— Major tick mark valuesMajor tick mark values, specified as a 1-by-n numeric array.
Any array value that falls outside the Limits
property
value is not displayed. MATLAB removes duplicate array values.
Setting the MajorTicks
property value sets
the MajorTicksMode
property value to 'manual'
.
'MajorTickLabels'
— Labels on each major tick mark{'0','20','40','60','80','100'}
(default) | cell array of character vectorsLabels on each major tick mark, specified as a cell array of
character vectors. MATLAB uses each element as the label for
the major tick marks specified in the MajorTicks
property.
Setting MajorTickLabels
changes the MajorTickLabelsMode
value
to 'manual'
.
If you specify more MajorTickLabels
elements
than MajorTicks
elements, then MATLAB ignores
the extra labels.
If you specify fewer MajorTickLabels
elements
than MajorTicks
elements, then MATLAB leaves
the extra ticks unlabeled.
To create a blank label to a major tick, specify an empty character
vector for that tick's corresponding MajorTickLabels
element.
Example: {'100','80','60','40','20','0'}
Example: {'Min','Max'}
'ValueChangedFcn'
— Code to execute when app user changes slider value''
(default) | function handle | cell array | character vectorCode to execute when the app user changes the slider value, specified as one of these values:
Function handle
Cell array containing a function handle and additional arguments
Character vector that is a valid MATLAB expression, which is evaluated in the base workspace.
If you specify this property as a function handle (or cell array containing a function handle), MATLAB passes an event object containing callback data as an argument to the callback function. This object contains the properties described in the following table. You can access these properties inside the callback function using dot notation.
Event Property | Value |
---|---|
PreviousValue | Value of slider before app user's most recent interaction with it. |
Value | Value of slider after app user's most recent interaction with it. |
Source | Handle to slider that executes the ValueChangedFcn callback. |
EventName | 'ValueChanged' |
For example, to display the previous and current value of the
slider in the MATLAB Command Window, specify this code in the ValueChangedFcn
callback:
event.PreviousValue event.Value
This callback function does not execute if the slider value changes programmatically.
Example: @myfunc
Example: {@myfunc, x}
'ValueChangingFcn'
— Code to execute as app user changes slider value''
(default) | function handle | cell array | character vectorCode to execute as the app user changes the slider value, specified as one of these values:
Function handle
Cell array containing a function handle and additional arguments
Character vector that is a valid MATLAB expression, which is evaluated in the base workspace.
If you specify this property as a function handle (or cell array containing a function handle), MATLAB passes an event object containing callback data as an argument to the callback function. This object contains the properties described in the following table. You can access these properties inside the callback function using dot notation.
Event Property | Value |
---|---|
Value | Current value of the slider as the app user is interacting with it. |
Source | Handle to the component that executes the ValueChangingFcn callback. |
EventName | 'ValueChanging' |
For example, to display the value of the slider in the MATLAB Command
Window, specify this code in the ValueChangingFcn
callback:
event.Value
The Value
property of the slider is not
updated until the app user releases the slider thumb. Therefore, to
get the slider values as the thumb is being moved, your code must
get the event.Value
rather than the slider object Value
.
The callback executes as follows:
If the app user clicks the slider value once. then the callback executes a single time. For example, if the slider is on 1.0, and the app user single-clicks at 1.1, then the callback executes once.
If the app user clicks and drags the slider to a new position, the callback executes repeatedly. For example, if the slider value is 1.0, and the app user clicks, holds, and drags the thump to value 10.0, then the callback executes multiple times until the app user releases the thumb.
When the slider Value
property changes
programmatically, the callback does not execute.
Example: @myfunc
Example: {@myfunc, x}
'Position'
— Location and size of slider [100 100 150 3]
(default) | [left bottom width height]
Location and size of the slider excluding tick marks and labels,
specified as the vector [left bottom width height]
.
This table describes each element in the vector.
Element | Description |
---|---|
left | Distance from the inner left edge of the parent container to the outer left edge of the slider |
bottom | Distance from the inner bottom edge of the parent container to the outer bottom edge of the slider |
width | Distance between the right and left outer edges of the slider |
height | Distance between the top and bottom outer edges of the slider |
All measurements are in pixel units.
You cannot change the height of a slider when the Orientation
property
value is 'horizontal'
. Similarly, you cannot change
the width of a slider when the Orientation
property
value is 'vertical'
.
Note:
The |
Example: [100 200 60 60]
fig = uifigure; sld = uislider(fig);
Create a uifigure window containing a panel. Create a slider and specify its position within the panel.
fig = uifigure;
pnl = uipanel(fig);
sld = uislider(pnl,'Position',[50 50 150 3]);
Create a slider. Set the Value
property
to 50.
fig = uifigure;
sld = uislider(fig,'Value',50);
Determine the current slider limits.
limits = sld.Limits
limits = 0 100
Change the slider limits and set the value to 35.
sld.Limits = [-50 50]; sld.Value = 35;
Create a slider and a gauge. When an app user moves the slider thumb and releases the mouse button, the needle of the gauge reflects the slider value.
Save the following code to sliderValue.m
on
your MATLAB path.
This code creates a UI figure window containing a slider and
a gauge. When an app user moves the slider thumb, the ValueChangedFcn
callback
for the slider updates the gauge to reflect the slider value.
function slidervalue % Create UI figure window and components fig = uifigure('Position',[100 100 350 275]); cg = uigauge(fig,'Position',[100 100 120 120]); sld = uislider(fig,... 'Position',[100 75 120 3],... 'ValueChangedFcn',@(sld,event) updateGauge(sld,cg)); end % Create ValueChangedFcn callback function updateGauge(sld,cg) cg.Value = sld.Value; end
Run sliderValue
, and then move the
slider thumb. When you release the mouse button, the circular gauge
needle moves to the matching value on the gauge.
Create a slider and a gauge. As an app user moves the slider thumb, the needle of the gauge reflects the changing slider value
This code creates a UI figure window containing a slider and
a gauge. As an app user moves the slider thumb, the ValueChangingFcn
callback
for the slider updates the gauge to reflect the slider value.
Save the following code to sliderChanging.m
on
your MATLAB path.
function sliderchanging % Create UI figure window and components fig = uifigure('Position',[100 100 350 275]); cg = uigauge(fig,'Position',[100 100 120 120]); sld = uislider(fig,... 'Position',[100 75 120 3],... 'ValueChangingFcn',@(sld,event) sliderMoving(event,cg)); end % Create ValueChangingFcn callback function sliderMoving(event,cg) cg.Value = event.Value; end
Run sliderChanging
, and then move the
slider. As you move the slider, the circular gauge needle moves, reflecting
the slider value.