Main Content
Control Color of Lamp on Instrument Panel by Using MATLAB®
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';
Create Target Object and Connect
Create a Target object for the default target computer and connect to the target computer.
tg = slrealtime; connect(tg);
Build Real-Time Application
model = 'slrt_ex_lamp_instrument'; open_system(model); modelSTF = getSTFName(tg); set_param(model,"SystemTargetFile",modelSTF) set_param(model,'FixedStep','.01'); evalc('slbuild(model)');
Run Real-Time Application
load(tg,model); start(tg);
Wait for Application Start on Target Computer
pause(2);
Add Instruments to UI Figure
inst = slrealtime.Instrument; inst.connectScalar(lamp1, 'lamp1', 'PropertyName', 'Color', 'Callback', @setLampColor); inst.connectScalar(lamp2, 'lamp2', 'PropertyName', 'Color'); addInstrument(tg,inst);
Wait and Observe Lamp Color Changes
pause(10);
Stop Real-Time Application
stop(tg);
Remove Instruments
removeInstrument(tg,inst);
Close Model
bdclose(model);
Callback for changing Lamp Color
function color = setLampColor(~,d) switch uint8(d(end)) 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