How can I make multiple objects flash with the same function in matlab app designer?

2 次查看(过去 30 天)
Hi All,
I was hoping someone would be able to help me with a GUI I am creating at the moment. The idea is that I have a few gauges with a value displayed underneath each of them with a coloured background.
each box represents the background that has the gauge with its corresponding value.
____________ ___________
| GAUGE 1 | | GAUGE 2 |
| VALUE 1 | | VALUE 2 |
-------------------- --------------------
What I want to do is make a box flash red when its value exceeds a threshold. I can get the code below to work with one block, but I want it so that if any value of any block exceeds its threshold they'll flash red. The problem is that when another block triggers its while loop, it interrupts the first one. I need it to happen in addition to whatever is already happening.
I will post the code for the VALUE1 block, but note that the VALUE2 block code is identical, just says Item2 instead.
Here's the code upon a change in a value of VALUE 1.
function Item1ValueValueChanged(app, event)
value = app.Item1Value.Value;
app.Item1Gauge.Value = value;
%Red Limit Flash
while value > 120
app.BlockFlashRed(app.ItemBlock);
value = app.Item1Value.Value;
pause(0.01)
end
%Yellow limit flash
while value > 100 && value <=120
app.BlockFlashYellow(app.ItemBlock);
value = app.ItemValue.Value;
pause(0.01)
end
%Reset
app.ItemBlock.BackgroundColor = [0.5,0.5,0.5];
end
which calls this function to sort of pulse red:
function BlockFlashRed(app,handle)
for ii = 0:0.01:1
handle.BackgroundColor = [ii,0,0];
pause(0.01);
end
end
I am quite new to this sort of GUI development so any help on how to set this up so I can have multiple blocks flashing red in parallel would be much appreciated!

回答(1 个)

Sameer
Sameer 2025-3-13
编辑:Sameer 2025-3-13
To enable each gauge block to flash independently without interruption, we can use "timer" objects. This allows asynchronous execution, meaning each block can handle its flashing behavior separately from others. When a block's value exceeds a specified threshold, its timer will trigger, causing the block to flash red. This ensures that multiple blocks can flash simultaneously without interfering with each other.
Here's how you can do it:
% Define the function to handle value changes for Item1
function Item1ValueValueChanged(app, event)
value = app.Item1Value.Value;
app.Item1Gauge.Value = value;
% Stop the timer if it's already running
if isfield(app, 'Item1Timer') && strcmp(app.Item1Timer.Running, 'on')
stop(app.Item1Timer);
end
% Create a new timer
app.Item1Timer = timer('ExecutionMode', 'fixedRate', ...
'Period', 0.1, ...
'TimerFcn', @(~,~) TimerCallback(app, 'Item1', value));
% Start the timer based on the value
if value > 120
start(app.Item1Timer);
elseif value > 100
%%
else
% Reset the background color
app.ItemBlock.BackgroundColor = [0.5, 0.5, 0.5];
end
end
% Define the timer callback function
function TimerCallback(app, item, value)
% Determine which block to update
switch item
case 'Item1'
handle = app.ItemBlock;
% Add cases for other items as needed
end
% Flash red
persistent toggle;
if isempty(toggle)
toggle = false;
end
toggle = ~toggle;
if toggle
handle.BackgroundColor = [1, 0, 0]; % Red
else
handle.BackgroundColor = [0.5, 0.5, 0.5]; % Default
end
% Stop the timer if the value is within the normal range
if value <= 120
stop(app.Item1Timer);
handle.BackgroundColor = [0.5, 0.5, 0.5]; % Reset color
end
end
For more details, refer to the MathWorks documentation link:
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Simulink 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by