How to use App Designer helper functions as Simulink callback methods.

1 view (last 30 days)
I am running a simulink model from app designer which plots the simulink output. For this I'm using Event Listener method approach.
The code I used in Simulink 'StartFcn' is :
blk2 = 'SineWave/Scope';
event = 'PostOutputs';
listener = @updateAxes;
% Create the listener
h1 = add_exec_event_listener(blk2, event, listener);
Here the updateAxes is an .m script that updates the App Designer axes. This runs without errors as a separate script.
But, I want to create a helper function in my app and use it as the above listener 'updateAxes' function. is it possible?

Accepted Answer

Walter Roberson
Walter Roberson on 19 Jul 2021
"Simulink software can register a listener only while a simulation is running. Invoking this function when no simulation is running results in an error message. To ensure that a listener catches all relevant events triggered by a model's simulation, you should register the listener in the model's StartFcn callback function (see Callbacks for Customized Model Behavior)."
That tells us that you cannot directly register the listener from within App Designer.
And that means in turn that the challenge is that when you do the @updateAxes equivalent, that the function you register must be visible inside the StartFcn.
It appears to me that app designer subclasses from handle. In theory if you had a small function or script that invoked the class, you could write the handle into the base workspace, where in theory it should be accessible to Simulink. Or if you do not want to use a small wrapper, then in theory you could have your App assignin('base') to assign a variable that is directly a handle to the function you want to invoke, like
blk2 = 'SineWave/Scope';
event = 'PostOutputs';
listener = Listener_Handle_Stored_by_App;
% Create the listener
h1 = add_exec_event_listener(blk2, event, listener);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!