Main Content


Create slider switch, rocker switch, or toggle switch component


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

sw = uiswitch(style) creates a switch of the specified style.


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


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


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


collapse all

fig = uifigure;
sliderswitch = uiswitch(fig);

fig = uifigure;
toggleswitch = uiswitch(fig,'toggle');

Create a rocker switch in a panel.

fig = uifigure;
pnl = uipanel(fig);
rockerswitch = uiswitch(pnl,'rocker');

Create a rocker switch.

fig = uifigure;
rockerswitch = uiswitch(fig,'rocker');

Change the switch text.

rockerswitch.Items = {'Stop','Start'};

Determine the current switch value.

val = rockerswitch.Value
val =


Save the following code as lampswitch.m on your MATLAB path. This code creates an app containing a lamp and a rocker switch. When the user flips the switch, the ValueChangedFcn callback changes the lamp color.

function lampswitch
fig = uifigure('Position',[100 100 370 280]);

lmp = uilamp(fig,...
    'Position',[165 75 20 20],...

sw = uiswitch(fig,'toggle',...
    'Position',[165 160 20 45],...

% ValueChangedFcn callback
function switchMoved(src,event)  
    switch src.Value
        case 'Go'
            lmp.Color = 'green';
        case 'Stop'
            lmp.Color = 'red';

Run lampswitch, and click the switch to see the color change.

Input Arguments

collapse all

Style of switch, specified as a value from the following table:


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 comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'Text',{'0','1'} specifies the two switch states are “0” and “1”.

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

Introduced in R2016a