The target object has many events the user can listen for, such as when the real-time target is connected, loaded, started, or stopped. To list the available target object events for your release, use the events(tg) function:
>> tg = slrealtime;
>> events(tg)
New R2024a Features for Adding Target Event Listeners to an App:
In R2024a, two new features were added to ease adding target event listeners to SLRT apps:
- The Target Events button in the SLRT App Generator lets you add callback code for target computer events by using the Callback Editor and outputs your event callback code in the generated app. For more information, see Add Callbacks for Target Computer Events.
- The slrealtime.ui.tool.EventManager manages event listeners in your app and can be linked with a Target Selector to switch the listeners to a new target when the user makes a selection in the drop-down.
.
Code Example for Adding Target Event Listeners to an App:
To add event listener code to your app in previous versions, or add it manually, the attached "TargetListenerApp.mlapp" (created with R2023a) shows an example of how to listen for 'started' and 'stopped' target events & to add callback code to an SLRT app:
- Define listeners as private properties and the related callback functions as private methods:
properties (Access = private)
tg slrealtime.Target
listenerStarted event.listener
listenerStopped event.listener
end
methods
function StartedFcn(app, event)
disp('Simulation started callback')
end
function StoppingFcn(app, event)
disp('Simulation stopped callback')
end
end
- Add listeners to the target object in the startupFnc():
function startupFcn(app)
app.tg = slrealtime;
app.listenerStarted = addlistener(app.tg,'Started',@(~,~)app.StartedFcn);
app.listenerStopped = addlistener(app.tg,'Stopped',@(~,~)app.StoppingFcn);
end
- Delete listeners in the UIFigureCloseRequest():
function UIFigureCloseRequest(app, event)
delete(app.listenerStarted)
delete(app.listenerStopped)
delete(app)
end
Note that these listeners are attached to whatever the default target is at the time the app is started. Changing the selected target in the Target Selector will have no effect on the listeners. To attach the listeners to a specific target, use app.tg = slrealtime('TargetPCName').