MATLAB: How to change the color of a subsystem during simulation based on the value of an input signal in Simulink 7.3 (R2009a)

changecoloremlsimulinksubsystem

I would like the background color of my subsystem block to change during simulation, based on the value of an input to the subsystem.

Best Answer

This can be done by using an Embedded MATLAB function block inside the subsystem. The Embedded MATLAB function would have two inputs, the signal to be tested and also the threshold to be tested against. Assuming those two inputs, the following code can be used to set the background color of the outer subsystem block to red if the input signal value is greater than the threshold input value, and otherwise set the background color to white:
function y = fcn(u, t)
%#eml
eml.extrinsic('set_param','get_param','gcs');
y = u;
if u > t
set_param(get_param(gcs,'parent'),'BackgroundColor','red');
else
set_param(get_param(gcs,'parent'),'BackgroundColor','white');
end