MATLAB: How to connect to SLRT Target in App Designer with “slrt” command

Calling the "slrt" command from within App Designer does not seem to work.
I have an App created in App Designer to interface with SLRT machines. One of the buttons initiates the connection with the target by calling:
>> app.tg = slrt();
Then I check the connection with
>> app.tg.connected
However this always returns false. It seems like when the "slrt" command is called from within an App Designer callback, the connection does not seem to be made. However when I call the same command in the base workspace, it does create the connection with the target and everything works.
For an example to reproduce the issue, refer to the attached app "connectApp.mlapp".
1. Open "connectApp"
2. Click the "Connect" button
3. The connection status should be green if connected, red if not. You can also check in "slrtexplr".
4. Note that you can only change connection status by running "slrt" in the Command Window but not in the app callback.

Best Answer

The issue seems to be that "slrt" is not evaluated when it is returned into a structure. However, you can evaluate it whenever you call the "tg" object with no semicolon. For example, consider the following sequence:
>> a.t = slrt
a =
struct with fields:
t: [1×1 SimulinkRealTime.target]
>> a.t.connected
ans =
'No'
>> a.t
Target: skyNet
Connected = Yes
Application = loader
>> a.t.connected
ans =
'Yes'
So essentially in order to fix the app, you can add a simple "app.tg" call inside the app callback with no semicolon, like seen below. This will make sure that it gets evaluated and connected.
>> app.tg = slrt;
>> app.tg %add this line
Note: "Connected" here mainly refers to a "communication status" between development and target computers and there is no real physical connection. So you can always go ahead and ping / load a model and it should work even if the connected command is not accurately set.