MATLAB: How to create custom buttons that perform certain tasks in a Simulink block diagram

simulink

I want to create buttons that perform certain tasks and add those buttons to my Simulink block diagram. For example, I want to click on a custom button in my block diagram to build or execute the model.

Best Answer

You can use masked Subsystem blocks with appropriately defined callbacks as custom buttons in a Simulink block diagram. There are several examples of this implementation in the demo models provided with Simulink. The attached example further illustrates this approach:
1. Save and open the attached model, cbut.mdl, in MATLAB.
2. Select the block named "Action 1".
3. Select "Block Properties" from the Simulink "Edit" menu. The "Action 1" Block Properties dialog box appears.
4. Click the "Callbacks" tab and then select the "OpenFcn" callback from the "Callback functions list". The dialog displays the contents of the OpenFcn callback function. Observe that the callback function uses the DISP command to display a text string.
The buttons that you see are empty masked subsystems. If you select one of the blocks and then select "Look Under Mask" from the Simulink "Edit" menu, you will observe that the subsystem is empty. However, each block has a callback function that executes under specific conditions. For instance, the OpenFcn callback function executes when a user double-clicks the block. In this example, when a user double-clicks the "Action 1" block, the OpenFcn callback executes and displays the text "Perform Action 1". Similarly, when a user double-clicks the "Open VDP model" block, the OpenFcn callback executes and opens the VDP demo model.