How to change push button colour while the simulation is running

23 次查看(过去 30 天)
Hi,
I have some push buttons in GUI app which are used to toggle respective constants.
The constants should be changed as per our requirement while the simulink model is running or stopped.
So when the button is pushed along with the change in value, the button's color should be changed.
The below code is in public method of GUI app and the method can called by callback function of buttons.
constants are varied only between 1 and 0.
In code view:
(ignore disp() as it is used only for debugging purpose)
for i_fed = 1:length(all_objects) % calling all buttons
if contains(string(all_objects(i_fed).Tag), button_name) % matching the correct button name
SimStatus=get_param(app.Model,'SimulationStatus'); % getting current simulation status
disp("ok1")
if SimStatus == "running" || SimStatus == "paused"
disp("ok2")
rto=get_param(strcat(app.Model,'/',block_name),'RuntimeObject'); %current Status of block_name button's constant block value
x=rto.OutputPort(1).Data
disp("ok3")
if x == 0 || x == '0' % gray for Off and green for On
disp("ok4.1")
all_objects(i_fed).BackgroundColor= app.color_gray;
all_objects(i_fed).FontColor=app.color_white;
else
disp("ok4.3")
all_objects(i_fed).BackgroundColor=app.color_green;
all_objects(i_fed).FontColor=app.color_white;
end
else
disp("ok6")
rto=get_param(strcat(app.Model,'/',block_name),'Value');
disp("ok7")
if rto=='0' % gray for Off and green for On
all_objects(i_fed).BackgroundColor= app.color_gray;
all_objects(i_fed).FontColor=app.color_white;
else
all_objects(i_fed).BackgroundColor=app.color_green;
all_objects(i_fed).FontColor=app.color_white;
end
end
break;
end
end
when the simulation is running If-block is executed and if simulation is stopped the else block should be executed.
color and value are being changed perfectly while simulation is stopped (upon pressing the button)
But only value is being changed while simulation is running but the color is not changing, the color is getting updated once the simulation is stopped
Here I got stuck till disp("ok4.1") and disp("ok4.2") the block is executed rest is not,
So how to change the color of button while the simulation is running? please let me know if there is any way, Thanks in advance...

采纳的回答

Cris LaPierre
Cris LaPierre 2024-11-8,13:58
Try adding drawnow following the code that changes the color. This tells MATLAB to update the figure now.

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by