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.

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 component in a new UI figure window and returns the Switch object.

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

example

sw = uiswitch(parent) creates the switch 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

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.

Input Arguments

collapse all

Type of switch, specified as one of the following styles:

StyleDefault Visual
'slider'
'rocker'
'toggle'

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

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.

Introduced in R2016a

Was this topic helpful?