Main Content

uiswitch

スライダー スイッチ、ロッカー スイッチまたはトグル スイッチの各コンポーネントを作成

説明

sw = uiswitch は、スライダー スイッチを新しい Figure ウィンドウ内に作成し、Switch オブジェクトを返します。MATLAB® は関数 uifigure を呼び出してこの Figure を作成します。

sw = uiswitch(style) は指定されたスタイルのスイッチを作成します。

sw = uiswitch(parent) は、指定された親コンテナー内にスイッチを作成します。親には、関数 uifigure を使用して作成された Figure か、またはその子コンテナーのいずれかを指定できます。

sw = uiswitch(parent,style) は、指定された親コンテナー内に指定されたスタイルのスイッチを作成します。

sw = uiswitch(___,Name,Value) は、1 つ以上の Name,Value のペアの引数を使用して、オブジェクトのプロパティを指定します。このオプションは、前述の構文のすべての入力引数の組み合わせで使用できます。

すべて折りたたむ

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.

パネル内にロッカー スイッチを作成します。

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.

ロッカー スイッチを作成します。

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

スイッチのテキストを変更します。

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

現在のスイッチの値を調べます。

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.

次のコードを MATLAB パス上に lampswitch.m として保存します。次のコードはランプとロッカー スイッチを含むアプリを作成します。ユーザーがスイッチを切り替えると、ValueChangedFcn コールバックがランプの色を変更します。

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

lampswitch を実行し、スイッチをクリックして色が変更されることを確認します。

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.

入力引数

すべて折りたたむ

スイッチのスタイル。次の表の値として指定します。

スタイル外観
'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.

親コンテナー。関数 uifigure を使用して作成された Figure オブジェクト、またはその子コンテナー (TabPanelButtonGroup または GridLayout) のいずれかとして指定します。親コンテナーを指定しない場合、MATLAB は関数 uifigure を呼び出し、親コンテナーとして機能する新しい Figure オブジェクトを作成します。

名前と値の引数

引数のオプションのペアを Name1=Value1,...,NameN=ValueN として指定します。ここで Name は引数名で、Value は対応する値です。名前と値の引数は他の引数の後になければなりませんが、ペアの順序は重要ではありません。

R2021a より前では、コンマを使用してそれぞれの名前と値を区切り、Name を引用符で囲みます。

例: 'Text',{'0','1'} は、スイッチの 2 つの状態として “0" および “1" を指定します。

各タイプのスイッチは、異なるプロパティのセットをサポートします。プロパティの完全なリストと各タイプについての説明は、対応するプロパティのページを参照してください。

バージョン履歴

R2016a で導入

すべて展開する