Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

uislider

(App Designer) 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.

Syntax

sld = uislider
sld = uislider(parent)
sld = uislider(___,Name,Value)

Description

sld = uislider creates a slider in a new UI figure window and returns the Slider object.

example

sld = uislider(parent) creates the slider in the specified parent container. The parent container can be a Figure created using the uifigure function, or one of its child containers: Tab, Panel, or ButtonGroup.

example

sld = uislider(___,Name,Value) specifies Slider properties using one or more Name,Value pair arguments. Use this option with any of the input argument combinations in the previous syntaxes.

Input Arguments

collapse all

Parent object, specified as a Figure created using the uifigure function, or one of its child containers: Tab, Panel, or ButtonGroup.

Name-Value Pair Arguments

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.

Example: '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.

collapse all

Slider value, specified as a numeric value. The numeric value must be within the range specified by the Limits property value.

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.

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'.

Major tick labels, specified as a cell array of character vectors. 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 ('') for the corresponding element in the MajorTickLabels property.

If the length of the MajorTickLabels cell 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'.

Code to execute when slider value changes, 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.

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.

PropertyValue
ValueValue of slider after app user’s most recent interaction with it
PreviousValueValue of slider before app user’s most recent interaction with it
SourceComponent that executes the callback
EventName'ValueChanged'

This callback function does not execute if the slider value changes programmatically.

Code to execute as the slider value changes, 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.

This callback function 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.

The following table lists the properties of the ValueChangingData object.

PropertyValue
ValueCurrent value of the slider as the app user is interacting with it
SourceComponent that executes the callback
EventName'ValueChanging'

The Value property of the Slider object is not updated until the app 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.

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 thump to value 10.0, then the callback executes multiple times until the app user releases the thumb.

When the Value property changes programmatically, the callback does not execute.

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.

ElementDescription
leftDistance from the inner left edge of the parent container to the outer left edge of the slider
bottomDistance from the inner bottom edge of the parent container to the outer bottom edge of the slider
widthDistance between the right and left outer edges of the slider
heightDistance 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]

Examples

collapse all

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 window containing a slider and a gauge. When an app user moves the slider thumb, the ValueChangedFcn callback 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 window containing a slider and a gauge. As an app user moves the slider thumb, the ValueChangingFcn callback 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.

See Also

Functions

Properties

Introduced in R2016a

Was this topic helpful?