Main Content

Control Color of Lamp on Instrument Panel

This example shows how to control the color of a lamp indicator on an instrument panel that connects to a Simulink Real-Time application.

The example operations are:

  • Create uifigure, Add Lamps, and Add Labels

  • Open Model and Build Real-Time Application

  • Connect Lamps and Add Instrument

  • Observe Color Cycle of Lamps

  • Remove Instrument

  • Close Model

Create uifigure and Add Components

f = uifigure;
lamp1 = uilamp(f);
lamp1.Position = [10 300 20 20];
tlabel1 = uilabel(f);
tlabel1.Position = [40 298 100 22];
tlabel1.Text = 'Lamp 1';
lamp2 = uilamp(f);
lamp2.Position = [10 200 20 20];
tlabel2 = uilabel(f);
tlabel2.Position = [40 198 100 22];
tlabel2.Text = 'Lamp 2';

Build and Run Real-Time Application

model = 'slrt_ex_lamp_instrument';
open_system(model);
evalc('slbuild(model)');
tg = slrealtime;
load(tg,model);
start(tg);
pause(2);
inst = slrealtime.Instrument;
inst.connectScalar(lamp1, 'lamp1', 'Property', 'Color', 'Callback', @setLampColor);
inst.connectScalar(lamp2, 'lamp2', 'Property', 'Color');
addInstrument(tg,inst);
pause(10);
stop(tg);
removeInstrument(tg,inst);
bdclose('all');

function color = setLampColor(~,d)
    switch uint8(d)
        case 5
            color = 'green';
        case 4
            color = 'yellow';
        case 3
            color = 'cyan';
        case 2
            color = 'magenta';
        case 1
            color = 'red';
        otherwise
            color = 'white';
    end
end