MATLAB Examples

Signal Tracing Using Scope Triggering

This example shows how to trace signals with a scope triggered Simulink® Real-Time™ host scope. After the script builds and downloads the oscillator model, xpcosc, it adds two scopes of type 'host' to the real-time application. The first scope is configured to trigger on the signal 'Signal Generator' (the only signal added to this scope). The 'Integrator1' signal is also added to the second scope. Scope 2 is configured to be triggered by the first scope (i.e., it is triggered at the same time the first scope is triggered). This synchronizes the scopes.

Next, the scopes are started and monitored to determine when data acquisition is complete. Data from both scopes are then uploaded to the development computer and plotted. Although both scopes begin data acquisition at the same time, Scope 2 acquires data over a longer time record by increasing the decimation factor from 4 to 10. This process repeats 25 times. After every fifth run, the damping gain 'Gain1/Gain' is set to a new random value (between 0 and 2000).


Check Connection Between Development and Target Computers

Use 'slrtpingtarget' to test the connection between the development and target computers.

if ~strcmp(slrtpingtarget, 'success')

Open, Build, and Download Model to the Target Computer

Open the oscillator model, xpcosc. Under the model's configuration parameter Simulink Real-Time option settings, the system target file has been set to slrt.tlc. Hence, building the model will create an executable image, xpcosc.mldatx, that can be run on a computer booted with the Simulink Real-Time kernel.


Build the model and download the image, xpcosc.mldatx, to the target computer.

  • Configure for a non-Verbose build.
  • Build and download application.
### Starting Simulink Real-Time build procedure for model: xpcosc
### Generated code for 'xpcosc' is up to date because no structural, parameter or code replacement library changes were found.
### Successful completion of build procedure for model: xpcosc
### Created MLDATX ..\xpcosc.mldatx
### Looking for target: TargetPC1
### Download model onto target: TargetPC1

Run Model, Randomize 'Gain' Parameter, Plot Host Scope Data

Create the MATLAB® variable, tg, containing the Simulink Real-Time target object. This object allows you to communicate with and control the target computer.

  • Create a Simulink Real-Time target object
  • Set sample time to 250us
  • Set stop time to a high value
  • Start model execution
tg =;
tg.SampleTime = 0.000250;
tg.StopTime   = 10000;

Create, configure, and plot to the host scope during each run.

  • Get index of parameter 'Gain1/Gain'
  • Get index of signal 'Integrator1'
  • Get index of signal 'Signal Generator'
tPar = getparamid(tg, 'Gain1', 'Gain');
signals(1) = getsignalid(tg, 'Integrator1');
signals(2) = getsignalid(tg, 'Signal Generator');
  • Define (add) first host scope object
  • Define (add) second host scope object
scs    = addscope(tg, 'host');
scs(2) = addscope(tg, 'host');

Set properties of first scope object

  • Add 'Signal Generator' to signal list
  • Set number of samples
  • Set decimation factor
  • Set trigger mode
  • Set trigger signal to 'Signal Generator'
  • Set trigger level
  • Set trigger slope
addsignal(scs(1), signals(2));
scs(1).NumSamples    = 200;
scs(1).Decimation    = 4;
scs(1).TriggerMode   = 'Signal';
scs(1).TriggerSignal = signals(2);
scs(1).TriggerLevel  = 0.0;
scs(1).TriggerSlope  = 'Rising';

Set properties of second scope object

  • Add both signals to signal list
  • Set number of samples
  • Set decimation factor
  • Set trigger mode
  • Set trigger scope to first scope object
scs(2).NumSamples    = 200;
scs(2).Decimation    = 10;
scs(2).TriggerMode   = 'Scope';
scs(2).TriggerScope  = scs(1).ScopeId;

Does the plot figure exist?

  • If no, create figure
  • If yes, make it the current figure
figh = findobj('Name', 'scscopedemo');
if isempty(figh)
  figh = figure;
  set(figh, 'Name','scscopedemo','NumberTitle','off');

Loop to acquire 25 data packages from the scope object.

  • Change parameter Gain1/Gain every fifth acquisition loop to a random value between 0 and 2000.
  • Start second scope (waits until triggered by first scope)
  • Start first scope
m = 1; flag = 0;
for n = 1 : 25
  if isempty(find(get(0, 'Children') == figh, 1)), flag = 1; break; end
  if ~m
    setparam(tg, tPar, 2*1000*rand);
  m = rem(m + 1, 5);

Wait until both scope objects have 'finished' state.

  while ~strcmpi(scs(1).Status,'finished') || ...

First scope object: create time vector, upload scope data and display it.

  • Upload time vector
  • Upload acquired data and plot
  subplot(2, 1, 1);
  t1 =  scs(1).Time;
  plot(t1, scs(1).Data, 'g');
  set(gca, 'XLim', [t1(1), t1(end)], 'YLim', [-10, 10]); ylabel('Scope 1');
  title(['scscopedemo: ', num2str(n), ' of 25 data packages']);

Second scope object: create time vector, upload scope data and display it.

  • Upload time vector
  • Upload acquired data and plot
  t2 =  scs(2).Time;
  plot(t2, scs(2).Data);
  set(gca,'XLim',[t2(1),t2(end)],'YLim',[-10,10]);  ylabel('Scope 2');
if ~flag
    subplot(2, 1, 1);
    title('scscopedemo: finished');

Stop and Close Model

When done, stop the application and close the model.

  • Stop model
  • Close model