How can I create a mouse controlled switch/button in Simulink 8.3 (R2014a) to route my signals?

1 view (last 30 days)
I want to be able to route my Simulink signals using a mouse click. The Manual Switch block can perform the routing, however, I want to improve the appearance. How can I create a mouse controlled switch/button in Simulink to route my signals with the following features?
1) Using the Mask OpenFcn callback for a subsystem
2) Using a MATLAB UI where the signal is held when the mouse is held

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 7 Jul 2014
Method 1 - Simulink native environment:
In the Simulink native environment, the only callback that can be triggered by the mouse click during model execution is the "OpenFcn" block callback. This callback is triggered when the block is double clicked.
 
Code can be added to the "OpenFcn" block callback to route the signal internally. To demonstrate one possible implementation using the "OpenFcn" callback, included is the example model, "buttonTrigger.slx". Under the OpenFcn callback for each "button" subsystem (Right click block > Properties > Callbacks Tab > OpenFcn), the following code has been added:
 
% Get subsystem block name and internal switch block name
blkName = gcb;
switchBlkName = [gcb '/ControlValue'];
 
% Get current value of switch
switchValue = get_param(switchBlkName,'Value');
switchValue = str2num(switchValue);
 
% Make changes according to previous state
if switchValue > 0,
set_param(switchBlkName,'Value','-1');
set_param(blkName,'BackgroundColor','red');  % Can change the color to indicate state
set_param(blkName,'MaskDisplay','disp(''Button On/Off\nOFF'')');
else
set_param(switchBlkName,'Value','1');
set_param(blkName,'BackgroundColor','green'); % Can change the color to indicate state
set_param(blkName,'MaskDisplay','disp(''Button On/Off\nON'')');
end
Method 2 - MATLAB UI with "WindowButtonUpFcn" and "WindowButtonDownFcn" callbacks:
To have greater control over how the mouse click is handled, a MATLAB UI that modifies the Simulink model should be used. The "WindowButtonUpFcn" and "WindowButtonDownFcn" callbacks can be specified for a MATLAB figure. When the figure is clicked (and held), the callback can route the signal accordingly. Once the mouse is released, the signal can be reverted to the default value.
For example, once a figure with handle 'h' has been declared, the callback functions can be declared:
set(h, 'WindowButtonUpFcn', @buttonUp);
set(h, 'WindowButtonDownFcn', @buttonDown); 
The "buttonUp" and "buttonDown" callbacks can be specified  using a similar code structure as Method 1:
function buttonUp(hObject,eventdata)
mdlName = gcs;
blkName = [mdlName '/MATLAB UI'];
switchBlkName = [blkName '/ControlValue'];
    
set_param(switchBlkName,'Value','-1');
set_param(blkName,'BackgroundColor','red');
set_param(blkName,'MaskDisplay','disp(''Button On/Off\nOFF'')');
end
function buttonDown(hObject,eventdata)
mdlName = gcs;
blkName = [mdlName '/MATLAB UI'];
switchBlkName = [blkName '/ControlValue'];
    
set_param(switchBlkName,'Value','1');
set_param(blkName,'BackgroundColor','green');
set_param(blkName,'MaskDisplay','disp(''Button On/Off\nON'')');
end
See the attached examples for more information.

More Answers (0)

Tags

No tags entered yet.

Products


Release

R2014a

Community Treasure Hunt

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

Start Hunting!