Main Content

addInstrument

Add instrument object to target object

Since R2020b

Description

example

addInstrument(target_object,instrument_object) adds an instrument object to the target object. Make sure that you add a signal to the instrument object before you add the instrument to the target object or no signal is streamed.

When the addInstrument() function binds the instrument to a real-time application, Simulink® Real-Time™ validates the signal names in the instrument. To reduce the time it takes to validate an instrument, add the instrument once to the target computer and run the real-time application as many times as needed. The instrument remains added to the target computer even after the real-time application stops and is reloaded.

Alternatively, you can add the instrument each time the real-time application runs. If using this approach, remove the instrument when the real-time application closes. For example, if adding code to an App Designer app, in the CloseRequestFcn, you could add a removeInstrument(hInst) function.

example

addInstrument(target_object,instrument_object,'updateWhileRunning') adds an instrument object to the target object and updates the target connection, even if the real-time application is running. Make sure that you add a signal to the instrument object before you add the instrument to the target object or no signal is streamed.

Examples

collapse all

Create a target object. Build the real-time application. Create the instrument object. Add a signal to the instrument object. Load the real-time application. Add an instrument object to the target object. Start real-time application.

tg = slrealtime('TargetPC1');
slbuild('slrt_ex_pendulum_100Hz');
hInst = slrealtime.Instrument('slrt_ex_pendulum_100Hz.mldatx');
hInst.addSignal('slrt_ex_pendulum_100Hz/cartposition',1)
load(tg,'slrt_ex_pendulum_100Hz');
addInstrument(tg,hInst);
start(tg);

To programmatically start and stop streaming a signal to the Simulation Data Inspector, you can use the addInstrument function and removeInstrument function.

% Create target object and build the real-time application.
tg = slrealtime;
connect(tg);
slbuild('slrt_ex_pendulum_100Hz');

% Create instrument object and add a signal to it. 
myInst = slrealtime.Instrument('slrt_ex_pendulum_100Hz.mldatx');
addSignal(myInst,'slrt_ex_pendulum_100Hz/cartposition',1); 

% Load the real-time application and start.
load(tg,'slrt_ex_pendulum_100Hz');
start(tg);

% To start streaming to the Simulation Data Inspector
% add the instrument to the target object.
addInstrument(tg,myInst); 

% To stop streaming the signal, you can use removeInstrument.
removeInstrument(tg,myInst);

Input Arguments

collapse all

Provides access to methods that manipulate the target computer properties.

Example: tg

To create the instrument object, use the Instrument function.

Example: hInst

Version History

Introduced in R2020b