uislider
Create slider component
Description
creates a slider in a new
figure window and returns the sld
= uisliderSlider
object. MATLAB® calls the uifigure
function to create the
figure.
specifies sld
= uislider(___,Name,Value
)Slider
properties using one or more name-value
arguments. For example, uislider("Value",50)
creates a slider
with a value of 50. Use this option with any of the input argument combinations in
the previous syntaxes.
Examples
Create Slider
Create Vertical Slider
Create a vertical slider in a UI figure.
fig = uifigure; sld = uislider(fig,"Orientation","vertical");
Set and Access Slider Properties
Create a slider in a UI figure. Set the slider value to 50.
fig = uifigure;
sld = uislider(fig,"Value",50);
Determine the current slider limits.
limits = sld.Limits
limits = 1×2
0 100
Change the slider limits and set the value to 35.
sld.Limits = [-50 50]; sld.Value = 35;
Configure Slider Tick Marks
Create a slider in a UI figure.
fig = uifigure; sld = uislider(fig);
Customize the slider appearance. Update the limits and major ticks to correspond to temperatures in degrees Fahrenheit and remove the minor ticks.
sld.Limits = [32 212];
sld.MajorTicks = [32 100 150 212];
sld.MajorTickLabels = sld.MajorTicks + "°F";
sld.MinorTicks = [];
Code Response to Moved Slider Thumb
Create an app with a slider and a gauge. When an app user moves the slider thumb and releases the mouse button, the needle of the gauge updates to reflect the slider value.
In a file named sliderApp.m
, write a function that implements the app:
Create a UI figure and a grid layout manager to lay out the app.
Create a gauge and a slider in the grid layout manager.
Write a callback function named
updateGauge
that changes the gauge value to match the slider value, and assign the function to theValueChangedFcn
callback property of the slider. For more information about callbacks, see Create Callbacks for Apps Created Programmatically.
function sliderApp fig = uifigure("Position",[100 100 300 250]); g = uigridlayout(fig); g.RowHeight = {'1x','fit'}; g.ColumnWidth = {'1x','fit','1x'}; cg = uigauge(g); cg.Layout.Row = 1; cg.Layout.Column = [1 3]; sld = uislider(g, ... "ValueChangedFcn",@(src,event)updateGauge(src,event,cg)); sld.Layout.Row = 2; sld.Layout.Column = 2; end function updateGauge(src,event,cg) cg.Value = event.Value; end
Run the sliderApp
function and move the slider thumb. When you release the thumb, the value of the gauge updates.
sliderApp
Code Response to Moving Slider Thumb
Create and app with a slider and a gauge. When an app user moves the slider thumb, the needle of the gauge immediately updates to reflect the slider value.
In a file named sliderApp2.m, write a function that implements the app:
Create a UI figure and a grid layout manager to lay out the app.
Create a gauge and a slider in the grid layout manager.
Write a callback function named
updateGauge
that changes the gauge value to match the slider value, and assign the function to theValueChangingFcn
callback property of the slider. For more information about callbacks, see Create Callbacks for Apps Created Programmatically.
function sliderApp2 fig = uifigure("Position",[100 100 300 250]); g = uigridlayout(fig); g.RowHeight = {'1x','fit'}; g.ColumnWidth = {'1x','fit','1x'}; cg = uigauge(g); cg.Layout.Row = 1; cg.Layout.Column = [1 3]; sld = uislider(g, ... "ValueChangingFcn",@(src,event)updateGauge(src,event,cg)); sld.Layout.Row = 2; sld.Layout.Column = 2; end function updateGauge(src,event,cg) cg.Value = event.Value; end
Run the sliderApp2
function and move the slider thumb. The gauge needle moves as you drag the slider thumb.
sliderApp2
Input Arguments
parent
— Parent container
Figure
object (default) | Tab
object | Panel
object | ButtonGroup
object | GridLayout
object
Parent container, specified as a Figure
object created using the uifigure
function or one of its child
containers: Tab
, Panel
, ButtonGroup
, or GridLayout
. If you do not specify a parent container, MATLAB calls the uifigure
function to create a new Figure
object that serves as the parent container.
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Example: uislider(Limits=[0 50])
specifies the minimum slider
value as 0
and the maximum slider value as
50
.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: uislider("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 Properties.
Value
— Slider value
0 (default) | numeric value
Slider value, specified as a numeric value. The numeric value
must be within the range specified by the Limits
property
value.
Data Types: double
Limits
— Minimum and maximum slider values
[0 100] (default) | two-element numeric array
Minimum 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
property
is less than the new lower limit, MATLAB sets the Value
property
to the new lower limit. For example, suppose the Limits
property
is [0 100]
and Value
is 20.
If the Limits
changes to [50 100]
,
then MATLAB sets the Value
property to 50.
Similarly, if you change Limits
such that
the Value
property is greater than the new upper
limit, MATLAB sets the Value
property to
the new upper limit.
Data Types: double
MajorTicks
— Major tick mark locations
[0 20 40 60 80 100]
(default) | vector of numeric values | []
Major tick mark locations, specified as a vector of numeric values or an empty vector. If you do not want to show major tick marks, specify this property as an empty vector.
Tick locations that are outside the range of the Limits
property
do not display.
MATLAB removes duplicate tick values. However, if a major tick falls on the same value as a minor tick, only the major tick displays.
Setting the MajorTicks
property sets the
MajorTicksMode
property to 'manual'
.
MajorTickLabels
— Major tick labels
{'0','20','40','60','80','100'}
(default) | cell array of character vectors | string array | {}
| ...
Major tick labels, specified as a cell array of character vectors, string array, or
1-D categorical array. If you do not want to show tick labels, specify this property as
an empty cell array. If you want to remove a label from a specific tick mark, specify an
empty character vector or empty string scalar for the corresponding element in the
MajorTickLabels
array. If you specify this property as a
categorical array, MATLAB uses the values in the array, not the full set of categories.
If the length of the MajorTickLabels
array is different from the
length of the MajorTicks
vector, MATLAB ignores the extra entries of the longer array. If there are extra labels,
they are ignored. If there are extra tick marks, they display without labels.
Setting MajorTickLabels
changes the
MajorTickLabelsMode
value to 'manual'
.
Note
Setting MajorTickLabels
when
MajorTicksMode
is 'auto'
might lead to
unexpected results. To avoid this behavior, set MajorTicksMode
to 'manual'
and manually specify the value of
MajorTicks
before setting
MajorTickLabels
.
ValueChangedFcn
— Value changed callback
''
(default) | function handle | cell array | character vector
Value changed callback, specified as one of these values:
A function handle.
A cell array in which the first element is a function handle. Subsequent elements in the cell array are the arguments to pass to the callback function.
A character vector containing a valid MATLAB expression (not recommended). MATLAB evaluates this expression in the base workspace.
This callback executes when the user moves the thumb to a different position on the slider. The callback does not execute if the slider value changes programmatically.
This callback function can access specific information about the user’s interaction
with the slider. MATLAB passes this information in a ValueChangedData
object as the second argument to your callback function.
In App Designer, the argument is called event
. You can query the
object properties using dot notation. For example,
event.PreviousValue
returns the previous value of the slider. The
ValueChangedData
object is not available to
callback functions specified as character vectors.
The following table lists the properties of the ValueChangedData
object.
Property | Value |
---|---|
Value | Value of slider after app user’s most recent interaction with it |
PreviousValue | Value of slider before app user’s most recent interaction with it |
Source | Component that executes the callback |
EventName | 'ValueChanged' |
For more information about writing callbacks, see Callbacks in App Designer.
ValueChangingFcn
— Value changing callback
''
(default) | function handle | cell array | character vector
Value changing callback, specified as one of these values:
A function handle.
A cell array in which the first element is a function handle. Subsequent elements in the cell array are the arguments to pass to the callback function.
A character vector containing a valid MATLAB expression (not recommended). MATLAB evaluates this expression in the base workspace.
This callback executes as the user moves the thumb along the slider in the app. It
does not execute if the Value
property changes
programmatically.
This callback can access specific information about the user’s interaction with the
slider. MATLAB passes this information in a ValueChangingData
object as the second argument to your callback
function. In App Designer, the argument is called event
. You can
query the object properties using dot notation. For example,
event.Value
returns the current value of the slider. The ValueChangingData
object is not available to callback
functions specified as character vectors.
This table lists the properties of the ValueChangingData
object.
Property | Value |
---|---|
Value | Current value of the slider as the app user is interacting with it |
Source | Component that executes the callback |
EventName | 'ValueChanging' |
The Value
property of the Slider
object is not updated until the user releases the slider thumb.
Therefore, to get the value as the thumb is being moved, your code must get the
Value
property of the ValueChangingData
object.
Note
Avoid updating the Value
property of the
Slider
object from within its own
ValueChangingFcn
callback, as this might result in unexpected
behavior. To update the slider value in response to user input, use a
ValueChangedFcn
callback instead.
The ValueChangingFcn
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 thumb to value 10.0, then the callback executes multiple times until the app user releases the thumb.
For more information about writing callbacks, see Callbacks in App Designer.
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'
.
The Position
values are relative to the
drawable area of the parent container. The drawable area is the area
inside the borders of the container and does not include the area occupied by decorations such
as a menu bar or title.
Example: [100 200 60 60]
Version History
Introduced in R2016a
Abrir ejemplo
Tiene una versión modificada de este ejemplo. ¿Desea abrir este ejemplo con sus modificaciones?
Comando de MATLAB
Ha hecho clic en un enlace que corresponde a este comando de MATLAB:
Ejecute el comando introduciéndolo en la ventana de comandos de MATLAB. Los navegadores web no admiten comandos de MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)