Main Content

uiswitch

Create slider switch, rocker switch, or toggle switch component

Description

s = uiswitch creates a slider switch in a new figure and returns the Switch object. MATLAB® calls the uifigure function to create the figure.

s = uiswitch(parent) creates a switch in the specified parent container. The parent can be a Figure object created using the uifigure function or one of its child containers.

example

s = uiswitch(style) creates a switch of the specified style. The switch style can be "slider", "rocker", or "toggle".

s = uiswitch(parent,style) creates a switch of the specified style in the specified parent container.

example

sw = uiswitch(___,Name,Value) specifies switch properties using one or more name-value arguments in addition to any of the input argument combinations in the previous syntaxes. For example, uiswitch("FontWeight","bold") creates a switch with bold label text.

example

Examples

collapse all

Create a slider switch in a UI figure.

fig = uifigure("Position",[100 100 300 300]);
s = uiswitch(fig);

Figure contains an object of type uiswitch.

Create a toggle switch in a UI figure.

fig = uifigure("Position",[100 100 300 300]);
ts = uiswitch(fig,"toggle");

Figure contains an object of type uitoggleswitch.

Create a UI figure with a panel. Create a rocker switch, specifying the panel as the parent container.

fig = uifigure("Position",[100 100 300 300]);
p = uipanel(fig);
rs = uiswitch(p,"rocker");

Figure contains an object of type uipanel.

Create a slider switch in a UI figure.

fig = uifigure("Position",[100 100 300 300]);
s = uiswitch(fig);

Figure contains an object of type uiswitch.

Change the switch label text by setting the Items property.

s.Items = ["Stop","Start"];

Figure contains an object of type uiswitch.

Determine the current switch value.

val = s.Value
val = 
'Stop'

Create a horizontal toggle switch in a UI figure.

fig = uifigure("Position",[100 100 300 300]);
ts = uiswitch(fig,"toggle","Orientation","horizontal");

Figure contains an object of type uitoggleswitch.

Create an app that changes the color of a lamp when a user toggles the value of a rocker switch.

In a file named updateLampColor.m, write a function that implements the app:

  • Create a UI figure and grid layout manager to lay out the app.

  • Create a lamp and a rocker switch in the grid layout manager.

  • Write a callback function named updateLamp that changes the lamp color based on the value of the switch, and assign the function to the ValueChangedFcn callback property of the switch. For more information about callbacks, see Create Callbacks for Apps Created Programmatically.

function updateLampColor
fig = uifigure("Position",[100 100 150 300]);
g = uigridlayout(fig);
g.RowHeight = {'1x','fit'};
g.ColumnWidth = {'1x'};

lmp = uilamp(g);

s = uiswitch(g,"rocker");
s.Items = ["Go","Stop"];
s.ValueChangedFcn = @(src,event) updateLamp(src,event,lmp);
end

function updateLamp(src,event,lmp)
val = src.Value;
switch val
    case "Go"
        lmp.Color = "green";
    case "Stop"
        lmp.Color = "red";
end
end

Run the updateLampColor function. Interact with the switch to change the color of the lamp.

updateLampColor

Figure contains an object of type uigridlayout.

Input Arguments

collapse all

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.

Style of switch, specified as a value from this table:

StyleAppearance
"slider"Slider switch, with a value of Off on the left and On on the right. The value of the switch is Off.
"rocker"Rocker switch, with a value of On at the top and Off at the bottom. The value of the switch is Off.
"toggle"Toggle switch, with a value of On at the top and Off at the bottom. The value of the switch is Off.

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: uiswitch(Items=["0","1"]) specifies the two switch states as "0" and "1".

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: uiswitch("Items",["0","1"]) specifies the two switch states as "0" and "1".

Each style of switch supports a different set of properties. For a full list of properties and descriptions for each style, see the associated switch component page.

Version History

Introduced in R2016a

expand all

See Also

Functions

Objects

Tools