Code covered by the BSD License  

Highlights from
Changing the color of a block while the simulation runs

image thumbnail

Changing the color of a block while the simulation runs

by

 

26 Feb 2009 (Updated )

Did you ever wanted to see the color of a block changing while a simulation is running?

ChangeColor(block)
function ChangeColor(block)
% Level-2 M file S-Function for times two demo.
%   Copyright 1990-2004 The MathWorks, Inc.
%   $Revision: 1.1.6.1 $ 

  setup(block);

function setup(block)
  
% Register number of input and output ports
  block.NumInputPorts  = 1;
  block.NumOutputPorts = 1;
  
  % Register number of parameters
  block.NumDialogPrms = 1;
  % Find and store destination block
  lines = get_param(gcb,'PortConnectivity');
  DestBlock = lines(2).DstBlock;
  set_param(gcb,'UserData',DestBlock);
  
% Setup functional port properties to dynamically inherited.
   block.SetPreCompInpPortInfoToDynamic;
   block.SetPreCompOutPortInfoToDynamic;
% Set block to be directfeedthrough  
  block.InputPort(1).DirectFeedthrough = true;
% Set block sample time to inherited
  block.SampleTimes = [-1 0];
  
% Register methods
  block.RegBlockMethod('Outputs',                 @Output);  
  block.RegBlockMethod('Terminate',             @Terminate); 

function Output(block)
  u = block.InputPort(1).Data;
  block.OutputPort(1).Data = u;
  Bname = get_param(gcb,'UserData');
  
  if u>block.DialogPrm(1).Data
      set_param(Bname,'BackgroundColor','Green');
  else
      set_param(Bname,'BackgroundColor','Red');
  end

function Terminate(block)
  Bname = get_param(gcb,'UserData');
  set_param(Bname,'BackgroundColor','white');

Contact us