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.
To specify the number of input ports:
Open a scope window.
From the toolbar, select File > Number of Input Ports > More.
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.
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.
Open a scope window and start the simulation.
Select Simulation > Simulink Snapshot.
The scope window status in the bottom left is Frozen, but the simulation continues to run in the background.
Interact with the paused display. For example, use measurements, copy the scope image, or zoom in or out.
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.
Add a scope block to your model.
Connect signals to scope ports. See Connect Multiple Signals to a Scope. For example, in the
vdp
model, connect the signalsx1
andx2
to a scope.Open the scope window and run the simulation.
Select File > Copy to Clipboard.
Paste the image into a document.
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.
Run the simulation using the main run button, then open up the ScopeA scope. The scope visualizes the data from the entire model.
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.
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.
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.
External, Accelerator, and Rapid modes — A scope plots data when the subsystem is enabled. The display connects the gaps with straight lines.
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 startup 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
.
Show Units on a Scope Display
From the Scope window toolbar, select the Configuration Properties button .
In the Configuration Properties: Scope dialog box, select the Display tab.
In the Y-label box, enter a title for the y-axis followed by
(%<SignalUnits>)
. For example, enterVelocity (%<SignalUnits>)
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
).
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
Get the scope properties. In the Command Window, enter
load_system('my_model') s = get_param('my_model/Scope','ScopeConfiguration');
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
.
From the Scope window toolbar, select the Configuration Properties button .
In the Configuration Properties window, select the Logging tab.
Select the Log data to workspace check box. In the text box, enter a variable name for saving simulation data. For example, enter
ScopeData
.From the Scope window toolbar, select the run button .
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
From a Scope window, select the Configuration Properties button .
In the Configuration Properties dialog box, select the Main tab, and then select the Layout button.
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.
Click to apply the selected layout to the Scope window.
Dock and Undock Scope Window to MATLAB Desktop
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.
Click the Show Scope Actions button, and then click Undock Scope.
See Also
Scope | Floating Scope and Scope Viewer