programing in Simulink a lamp
6 views (last 30 days)
Show older comments
i need the lamp to display different color when it gets certain value from the matlab function. i add another Matlab Function to program the lamp but i am not sure if that such a thing to exist.

this is simulink. Here is my code that is in the Matlab Function to program the lamp as you see how i connect the lamp to the function. the code below is the code inside the matlab function. i am not even sure if my code is right at all.
function lampStates = caution(distance)
lampState1.Value = ( distance >= 0.0) && ( distance <= 0.5);
lampState2.Value = ( distance == 0.5);
lampState3.Value = ( distance == 1.5);
lampState4.Value = ( distance == 2.5);
lampState5.Value = ( distance >= 2.5);
if ( distance >= 0.0) && ( distance <= 0.5)
lampState1.Color = [254 51 10];
elseif ( distance == 0.5)
lampState2.Color = [237 177 32];
elseif ( distance == 1.5)
lampState3.Color = [100 212 19];
elseif ( distance == 2.5)
lampState4.Color = [237 177 32];
elseif ( distance >= 2.5)
lampState5.Color = [254 51 10];
end
lampStates = [lampState1 lampState2 lampState3 lampState4 lampState5];
1 Comment
Walter Roberson
on 28 Jan 2025
lampState2.Value = ( distance == 0.5);
0.5 (and 1.5, and 2.5) are values that can be exactly represented in double precision. It is not uncommon that floating point calculations might end up with exact 0.5 or 1.5 or 2.5.
However... it is also not uncommon for floating point calcuations to end up with (say) 1.4999999999999966693309261245303787291049957275390625 instead of 1.5 exactly . So it is often not a good idea to test for 0.5, 1.5, 2.5 exactly but to instead test for ranges, or test for values within a tolerance. For example,
if distance > 1.25 & distance <= 1.75
or
if abs(distance - 1.5) < 1e-7
or (as of R2024b)
if isapprox(distance, 1.5)
Answers (1)
Kanishk
on 28 Jan 2025
Hi @ali
I understand you want to program the states and color of the Simulink lamp to visualize "distance" signal in Simulink dashboard itself. You can indeed use Simulink Lamp for this purpose but you do not need "MATLAB Function Block". You can write a script to define the "States" and "colors" of the lamp block.
To define the states for the Lamp block, you need set the ranges of values you want a color to be displayed. Here is a simple code to set the "States" and "Colors" of the Lamp block programmatically.
lampState1.Value = [0.0 0.5];
lampState2.Value = [0.5 1.5];
lampState3.Value = [1.5 2.5];
lampState4.Value = [2.5 inf];
lampState1.Color = [254 51 10]/256;
lampState2.Color = [237 177 32]/256;
lampState3.Color = [100 212 19]/256;
lampState4.Color = [237 177 32]/256;
lampStates = [lampState1 lampState2 lampState3 lampState4];
set_param(gcs+"/Lamp1", "StateValueType", "Range")
set_param(gcs+"/Lamp1", "StateColors", lampStates)
You can execute this code in MATLAB after opening the Simulink model or add this code in the "InitFcn" model callback using the Property Inspector.
You can learn more how to add code to model callbacks in Simulink following this official MathWorks documentation.
0 Comments
See Also
Categories
Find more on Interactive Model Editing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!