Karlo
Karlo

Reputation: 1708

How to execute a function by clicking on a button in Simulink?

I would like to insert a button in my Simulink file that runs a (.m) function when I double-click on it. How can this be achieved?

Upvotes: 1

Views: 3427

Answers (1)

Phil Goddard
Phil Goddard

Reputation: 10772

You want to create a block in your model, and use its OpenFcn callback. There's an example of doing almost exactly this in the Model Callbacks section of the documentation.

The basic steps are:

  1. Add a SubSystem block to your model. Typically you'll make this empty (i.e. have no inports or outports in it).
  2. Right click on the block and open its Properties menu.
  3. Go to the Callbacks tab, and select the OpenFcn callback.
  4. Insert the code you want to execute when you double click on the block. In your case just put the name of your function (assuming that it has no input arguments).

Most people would also then put a mask on the subsystem too.

Upvotes: 3

Related Questions