Nordlöw
Nordlöw

Reputation: 12138

Specifying Callbacks for Simulink Block Types

I'm trying to figure out how to set a callback for for every instance of a specific MATLAB object type and not a single instance. In my case the object type is either a SimuLink Block, Subsystem, or ModelReference.

After having read

http://se.mathworks.com/help/matlab/creating_plots/callback-definition.html

I tried

function openCallback(src, evt)
    disp('here');
end
set(groot, 'defaultBlockOpenFcn', @openCallback);

but it fails as

Error using matlab.ui.Root/set
blockopenfcn is an invalid class name

Is this possible, somehow?

Upvotes: 0

Views: 227

Answers (1)

Navan
Navan

Reputation: 4477

Simulink block callbacks are set using set_param function on a block handle. You would need to set it for a single instance of the block. I do not think it is possible to set it for every instance of a block. If you have a model you can find all the blocks of a type and set their callbacks in a loop.

You can see help for block callbacks at http://www.mathworks.com/help/simulink/ug/block-callbacks.html. You would call set_param as

set_param(gcb, 'OpenFcn', 'disp(''open fcn'')')

where gcb is a function that returns currently selected block in a model.

Upvotes: 3

Related Questions