Main Content

Common Scope Block Tasks

To visualize your simulation results over time, use a Scope block or Time Scope (DSP System Toolbox) block

Connect Multiple Signals to a Scope

To connect multiple signals to a scope, drag additional signals to the scope block. An additional port is created automatically.

Animated GIF of automatically created input ports on a scope block

To specify the number of input ports:

  1. Open a scope window.

  2. From the toolbar, select File > Number of Input Ports > More.

  3. Enter the number of input ports, up to 96.

Signals from Nonvirtual Buses and Arrays of Buses

You can connect signals from nonvirtual buses and arrays of buses to a Scope block. To display the bus signals, use normal or accelerator simulation mode. The Scope block displays each bus element signal, in the order the elements appear in the bus, from the top to the bottom. Nested bus elements are flattened. For example, in this model the nestedBus signal has the const, subSignal, and step signals as elements. The subSignal sub-bus has the chirp and sine signals as its bus elements. In the Scope block, the two elements of the subSignal bus display between the const and step signals.

Simulink model of a scope connected to the nestedBus, const, subSignal, chirp, sine, and step signals.

Scope window showing the four signals and their names in the Scope legend: nestedBus.const, nestedBus.subSignal.chirp, nestedBus.subSignal.sine, nestedBus.step.

Save Simulation Data Using Scope Block

This example shows how to save signals to the MATLAB Workspace using the Scope block. You can us these steps for the Scope or Time Scope blocks. To save data from the Floating Scope or Scope viewer, see Save Simulation Data from Floating Scope.

Using the vdp model, turn on data logging to the workspace. You can follow the commands below, or in the Scope window, click the Configuration Properties button and navigate to the Logging tab, turn on Log data to workspace.

open_system('vdp');
scopeConfig = get_param('vdp/Scope','ScopeConfiguration');
scopeConfig.DataLogging = true;
scopeConfig.DataLoggingSaveFormat = 'Dataset';
out = sim('vdp');

In the MATLAB Command window, view the logged data from the out.ScopeData structure.

x1_data = out.ScopeData{1}.Values.Data(:,1);
x1_time = out.ScopeData{1}.Values.Time;
plot(x1_time,x1_data)

Pause Display While Running

Use the Simulink Snapshot to pause the scope display while the simulation keeps running in the background.

  1. Open a scope window and start the simulation.

  2. Select Simulation > Simulink Snapshot.

    The scope window status in the bottom left is Frozen, but the simulation continues to run in the background.

  3. Interact with the paused display. For example, use measurements, copy the scope image, or zoom in or out.

  4. To unfreeze the display, select Simulation > Simulink Snapshot again.

Copy Scope Image

This example uses the vdp model to demonstrate how to copy and paste a scope image.

  1. Add a scope block to your model.

  2. Connect signals to scope ports. See Connect Multiple Signals to a Scope. For example, in the vdp model, connect the signals x1 and x2 to a scope.

  3. Open the scope window and run the simulation.

    Scope window displaying the simulation results of the vdp model

  4. Select File > Copy to Clipboard.

  5. Paste the image into a document.

    vdp scope visualization with a white background for printing

    By default, Copy to Clipboard saves a printer-friendly version of the scope with a white background and visible lines. If you want to paste the exact scope plot displayed, select View > Style, then select the Preserve colors for copy to clipboard check box.

Plot an Array of Signals

This example shows how the scope plots an array of signals.

In this simple model, a Sine Wave block is connected to a scope block. The Sine Wave block outputs four signals with the amplitudes [10, 20; 30 40]. The scope displays each sine wave in the array separately in the matrix order (1,1), (2,1), (1,2), (2,2).

Scopes in Referenced Models

This example shows the behavior of scopes in referenced models. When you use a scope in a referenced model, you see different output in the scope depending on where you started the simulation: from the top model or the scope in the referenced model.

Note

Scope windows display simulation results for the most recently opened top model. Playback controls in scope blocks and viewers simulate the model containing that block or viewer.

This example uses the sldemo_mdlref_counter model both as a top model and as a referenced model from the sldemo_mdlref_basic model.

Open the model:

openExample('simulink/FindMdlrefsFindReferencedModelsinModelReferenceHierarchyExample','supportingfile','sldemo_mdlref_basic')

Double-click the CounterA block. The sldemo_mdlref_counter model opens as a referenced model, as evidenced by the breadcrumb above the canvas.

sldemo_mdlref_counter model with the breadcrumb highlighted to show you are in the CounterA referenced model block from the sldemo_mdlref_basic model.

Run the simulation using the main run button, then open up the ScopeA scope. The scope visualizes the data from the entire model.

Scope window with model simulation results

If you specifically want to visualize a referenced model in isolation, open the model as a top model. In this example, right-click the CounterA block and select Open as Top Model. The model opens in another window and the breadcrumb only shows the referenced model name.

sldemo_mdlref_counter model with the breadcrumb highlighted to show you are in a non-referenced version of the model

When you run the simulation from either the Simulink window or the scope window, the scope visualizes the model without any reference to another model. In this case, the model input is zero the entire time.

Scope window showing a constant zero line

Scopes Within an Enabled Subsystem

When placed within an Enabled Subsystem block, scopes behave differently depending on the simulation mode:

  • Normal mode — A scope plots data when the subsystem is enabled. The display plot shows gaps when the subsystem is disabled.

    Sample scope window with gaps where the subsystem is disabled

  • External, Accelerator, and Rapid modes — A scope plots data when the subsystem is enabled. The display connects the gaps with straight lines.

    Sample scope window with straight lines where the subsystem is disabled

Modify x-axis of Scope

This example shows how to modify the x-axis values of the Scope block using the Time span and Time display offset parameters. The Time span parameter modifies how much of the simulation time is shown and offsets the x-axis labels. The Time display offset parameter modifies the labels used on the x-axis.

You can also use this procedure for the Time Scope block, Floating Scope block, or Scope viewer.

Open the model and run the simulation to see the original scope output. The simulation runs for 10 time steps stepping up by 1 at each time step.

model = 'ModifyScopeXAxis';
open_system(model);
sim(model);
open_system([model,'/Scope']);

Modify Time Span Shown

Modify the Time span parameter to 2. You can follow the commands below, or in the Scope window, click the Configuration Properties button and navigate to the Time tab.

scopeConfig = get_param([model,'/Scope'],'ScopeConfiguration');
scopeConfig.TimeSpan = '2';
sim(model);
open_system([model,'/Scope']);

The x-axis of the scope now shows only the last 2 time steps and offsets the x-axis labels to show 0-2. The bottom toolbar shows that the x-axis is offset by 8. This offset is different from the Time display offset value.

The Time span parameter is useful if you do not want to visualize signal initialization or other start-up tasks at the beginning of a simulation. You can still see the full simulation time span if you click the Span x-axis button.

Offset x-axis Labels

Modify the Time display offset parameter to 5. Again, use the commands below, or in the Scope window, click the Configuration Properties button and navigate to the Time tab.

scopeConfig.TimeDisplayOffset = '5';
sim(model);
open_system([model,'/Scope']);

Now, the same time span of 2 is show in the scope, but the x-axis labels are offset by 5, starting at 5 and ending at 7. If you click the Span x-axis button, the x-axis labels still start at 5.

Show Signal Units on a Scope Display

You can specify signal units at a model component boundary (Subsystem and Model blocks) using Inport and Outport blocks. See Unit Specification in Simulink Models. You can then connect a Scope block to an Outport block or a signal originating from an Outport block. In this example, the Unit property for the Out1 block was set to m/s.

Sample model with units specified

Show Units on a Scope Display

  1. From the Scope window toolbar, select the Configuration Properties button .

  2. In the Configuration Properties: Scope dialog box, select the Display tab.

  3. In the Y-label box, enter a title for the y-axis followed by (%<SignalUnits>). For example, enter

    Velocity (%<SignalUnits>)
    
  4. Click OK or Apply.

    Signal units display in the y-axis label as meters per second (m/s) and in the Cursor Measurements panel as millimeters per second (mm/s).

    Scope window with the units displayed in the cursor measurements and y-axis label.

From the Simulink toolstrip, you can also select Debug > Information Overlays > Units. You do not have to enter (%<SignalUnits>) in the Y-Label property.

Show Units on a Scope Display Programmatically

  1. Get the scope properties. In the Command Window, enter

    load_system('my_model')
    s = get_param('my_model/Scope','ScopeConfiguration');
    
  2. Add a y-axis label to the first display.

    s.ActiveDisplay = 1
    s.YLabel = 'Velocity (%<SignalUnits>)';
    

You can also set the model parameter ShowPortUnits to 'on'. All scopes in your model, with and without (%<SignalUnits>) in the Y-Label property, show units on the displays.

load_system('my_model')
get_param('my_model','ShowPortUnits')
ans =
off
set_param('my_model', 'ShowPortUnits','on')
ans =
on

Determine Units from a Logged Data Object

When saving simulation data from a scope with the Dataset format, you can find unit information in the DataInfo field of the timeseries object.

Note

Scope support for signal units is only for the Dataset logging format and not for the legacy logging formats Array, Structure, and Structure With Time.

  1. From the Scope window toolbar, select the Configuration Properties button .

  2. In the Configuration Properties window, select the Logging tab.

  3. Select the Log data to workspace check box. In the text box, enter a variable name for saving simulation data. For example, enter ScopeData.

  4. From the Scope window toolbar, select the run button .

  5. In the Command Window, enter

    ScopeData.getElement(1).Values.DataInfo
    Package: tsdata
    Common Properties:
                 Units: m/s (Simulink.SimulationData.Unit)
         Interpolation: linear (tsdata.interpolation)
    

Connect Signals with Different Units to a Scope

When there are multiple ports on a scope, Simulink® ensures that each port receives data with only one unit. If you try to combine signals with different units (for example by using a Bus Creator block), Simulink returns an error.

Scopes show units depending on the number of ports and displays:

  • Number of ports equal to the number of displays — One port is assigned to one display with units for the port signal shown on the y-axis label.

  • Greater than the number of displays — One port is assigned to one display, with the last display assigned the remaining signals. Different units are shown on the last y-axis label as a comma-separated list.

Select Number of Displays and Layout

  1. From a Scope window, select the Configuration Properties button .

  2. In the Configuration Properties dialog box, select the Main tab, and then select the Layout button.

  3. Select the number of displays and the layout you want.

    You can select more than four displays in a row or column. Click within the layout, and then drag your mouse pointer to expand the layout to a maximum of 16 rows by 16 columns.

    Animated GIF of modifying the layout of the scope window

  4. Click to apply the selected layout to the Scope window.

Dock and Undock Scope Window to MATLAB Desktop

  1. In the right corner of a Scope window, click the Dock Scope button.

    The Scope window is placed above the Command Window in the MATLAB® desktop.

  2. Click the Show Scope Actions button, and then click Undock Scope.

    Undock scope value in the dropdown menu

See Also

| |

Related Topics