This is machine translation

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

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

uiswitch

(App Designer) Create slider switch, rocker switch, or toggle switch component

Use only with App Designer or figures created with the uifigure function. Apps created with GUIDE or the figure function do not support slider switches, rocker switches, or toggle switches.

Syntax

sw = uiswitch
sw = uiswitch(style)
sw = uiswitch(parent)
sw = uiswitch(parent,style)
sw = uiswitch(___,Name,Value)

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);

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 =

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 350 275]);

% Create lamp
lmp = uilamp(fig,...
    'Position',[165 75 20 20],...
    'Color','green');

% Create switch
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.

Input Arguments

collapse all

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

StyleAppearance
'slider'
'rocker'
'toggle'

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

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