Stream Signal Data from Target Computer to Simulation Data Inspector

This example shows how to create a signal list for a Simulink Real-Time model by using the streaming signals API. After you build the real-time application from the model and run the application on the target computer, you can stream signal data to the Simulation Data Inspector from dynamically selected signals.

Create a Signal List Object for Blocks

Open the model, identify the programmatic names of blocks in the model, and create a signal list object. For more information, see SimulinkRealTime.SignalList.

open_system(fullfile(matlabroot,'toolbox','rtw','targets','xpc','xpcdemos','xpcosc'));
find_system('xpcosc')
mySignals=SimulinkRealTime.SignalList()
ans =

  11×1 cell array

    {'xpcosc'                  }
    {'xpcosc/Gain'             }
    {'xpcosc/Gain1'            }
    {'xpcosc/Gain2'            }
    {'xpcosc/Integrator'       }
    {'xpcosc/Integrator1'      }
    {'xpcosc/Mux'              }
    {'xpcosc/Scope'            }
    {'xpcosc/Signal↵Generator'}
    {'xpcosc/Sum'              }
    {'xpcosc/Outport'          }


mySignals = 

  SignalList with no properties.

Add Signals to Signal List Object

To add signals by name to the signal list object, get parameter information for the signals and use the parameter information in the add command. Adding signals by name makes it easier to find signals in the Simulation Data Inspector when viewing the signal data.

p = get_param('xpcosc/Integrator','PortHandles');
l = get_param(p.Outport,'Line');
set_param(l,'Name','Integ_out');
add(mySignals,'Integ_out')
p = get_param('xpcosc/Integrator1','PortHandles');
l = get_param(p.Outport,'Line');
set_param(l,'Name','Integ1_out');
add(mySignals,'Integ1_out')

You also can add signals to the signal list object by block path and port index. To add the xpcosc model Integrator block input to the signal list by block path and port index, the command is:

add(mySignals,'xpcosc/Integrator',1);

View Signals in Signal List Object

To view signals in the signal list object, use the view command.

view(mySignals)
Integ_out
Integ1_out

Build the Model and Run the Application

Build the model and download the real-time application to the target computer. After building the real-time application, you can close the model.

The model does not need to be open to stream signal data from the real-time application.

evalc('rtwbuild(''xpcosc'')');
tg = slrt('TargetPC1');
load(tg,'xpcosc');
bdclose('all');

Set the stop time and start the real-time application. To generate signal data, run the application for 20 seconds.

tg.StopTime=Inf;
start(tg);
pause(20);

Stream Signal data to Simulation Data Inspector

To select signals to stream, use the setStreamingSignals command. This signal selection with the signal list object is dynamic because the selection occurs after you build and download the real-time application.

After you select signals to stream, display the signals in the Simulation Data Inspector (SDI). For more information, see setStreamingSignals.

setStreamingSignals(tg,mySignals);
Simulink.sdi.view;

To stop streaming signals, use the command:

setStreamingSignals(tg,[]);

stop(tg);

See Also

Related Topics