Main Content

uiswitch

Create slider switch, rocker switch, or toggle switch component

Description

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.

example

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.

example

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

example

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.

Examples

collapse all

fig = uifigure;
sliderswitch = uiswitch(fig);

Slider switch in a UI figure window, with a value of Off on the left and On on the right. The value of the switch is Off.

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

Toggle switch in a UI figure window, with a value of On at the top and Off at the bottom. The value of the switch is off.

Create a rocker switch in a panel.

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

Rocker switch in a panel in a UI figure window, with a value of On at the top and Off at the bottom. The value of the switch is off.

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 =

    'Stop'

Rocker switch in a UI figure window, with a value of Start at the top and Stop at the bottom. The value of the switch is Stop.

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],...
    'Color','green');


sw = uiswitch(fig,'toggle',...
    'Items',{'Go','Stop'},...    
    'Position',[165 160 20 45],...
    'ValueChangedFcn',@switchMoved); 

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

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

Toggle switch and lamp in a UI figure window. The switch has values of Stop and Go, and is flipped to Go. The lamp is green.

Input Arguments

collapse all

Style of switch, specified as a value from the following 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.

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.

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

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.

Version History

Introduced in R2016a