Main Content

Visualize Simulation Data on XY Plot

This example shows how to plot data on an XY plot in the Simulation Data Inspector and use the replay controls to analyze relationships among plotted signals. You run two simulations of the model ex_sldemo_bounce and analyze the relationship between the position and velocity for a bouncing ball.

The XY plot used in this example is also available in the Record block and the XY Graph block. When you use the XY plot in the Record block and the XY Graph block, you add the visualization and configure the appearance using the toolstrip. You plot data on the XY plot the same way shown in this example.

Simulate Model

Open the model ex_sldemo_bounce, which simulates the dynamics of a bouncing ball.

The ex_sldemo_bounce model

Because the model is configured to log output data, the position and velocity signals connected to the Outport blocks log to the workspace and the Simulation Data Inspector.

To simulate the model, click Run. Then, under Review Results, click Data Inspector to open the Simulation Data Inspector.

Alternatively, you can simulate the model and open the Simulation Data Inspector programmatically.

mdl = "ex_sldemo_bounce";
out = sim(mdl);
Simulink.sdi.view

Plot Data on XY Plot

By default, the Simulation Data Inspector uses time plots for each subplot in the layout. To plot the data on an XY plot, add the visualization to the layout. Click Visualizations and layouts, then click or click and drag the XY icon onto the subplot.

The Visualizations and layouts menu

To plot the signals on the XY plot, in the signal table, select the signals you want to plot. Then, assign the signal pairs to the x- and y-axes. For this example:

  1. Select the check boxes next to the Position and Velocity signals.

  2. In the XY Data dialog box, use the drop-down menus in row 1 to assign Position to the x-Axis and Velocity to the y-Axis.

  3. Click OK.

The XY Data dialog box

You can also use the XY Data dialog box to:

  • Swap x and y data selections using the Swap button.

  • Add more pairs of x and y data using the drop-down menus in the next row.

You can modify the appearance of the XY plot using the Visualization Settings. To access the Visualization Settings for the XY plot, click Visualization SettingsVisualization Settings icon.

By default, data is shown as a scatter plot. The color of the marker fill matches the y-axis signal and the color of the marker border matches the x-axis signal color. You can select or clear Line and Markers to display only markers, only lines, or both markers and connecting lines. The line and marker colors can match the color of the signal that provides the x data or the signal that provides the y data. Specified settings apply for all XY plots in the layout.

The Visualization Settings menu

You can also change the limits of the x-axis and y-axis from the Visualization Settings. To change the limits, first, clear Autoscale. Then, enter new values for the x and y limits. These limits apply to the currently selected XY subplot. By default, Autoscale is selected, which performs a fit-to-view on the XY subplot.

The Limits section of the Visualization Settings menu

Add Time Plots and Inspect Data

You can include multiple visualizations in a layout in the Simulation Data Inspector, Record block, or XY Graph block. For example, change to a layout with three subplots so you can see each signal on a time plot alongside the XY plot.

To change the subplot layout, click Visualizations and layouts. Then, in the Basic Layouts section, select the layout with two subplots on top of a third subplot.

The Basic layouts section of the Visualizations and layouts menu is expanded to show the layout with two subplots on top of a third subplot.

Plot the Position signal on the upper-right time plot, and plot the Velocity signal on the bottom time plot. To plot a signal on a time plot, select the subplot where you want to plot the signal, then select the check box next to the signal you want to plot.

Simulation Data Inspector with a three-plot layout: The upper left subplot displays an XY plot of position and velocity, the upper right subplot shows a time plot of position, and the bottom subplot features a time plot of velocity.

To inspect the data, add a cursor. In the XY plot, the vertical line of the cursor shows the x-axis value, and the horizontal line shows the y-axis value. The time that corresponds to the point is displayed in the upper-right of the plot.

Move the cursor in the XY plot along the plotted line. You can also move the cursor in the XY plot using the arrow keys on your keyboard or by pausing on a point on the line and clicking the highlighted point.

The XY plot cursor shows a velocity of 10.6 at a position of -7.9.

When you drag the cursor in a time plot, the cursor in the XY plot moves synchronously through the plotted data. The XY plot can only have one cursor. When you add two cursors to the layout, the XY cursor moves with the left cursor in the time plot.

Replay Data

Now that you have a comprehensive visualization of the simulation data, replaying the data can help you understand the relationship between the signals. When you replay data in the Simulation Data Inspector, animated cursors sweep through the logged simulation data from the start time to the end time. Add the replay controls to the view by clicking Show/hide replay controls The Show/hide replay controls icon.

You can control the speed of the replay and pause at any time. By default, the Simulation Data Inspector replays data at one second per second, meaning the cursor moves through one second of data in one second of clock time. The data in this example spans 25 seconds. Slow the replay speed by clicking the arrow to the left of the label.

The Simulation Data Inspector displays cursors showing position and velocity at 9 seconds into the simulation with replay controls visible below the subplots.

For more information about using replay controls, see Replay Data in the Simulation Data Inspector.

Analyze Data from Multiple Simulations

To analyze how changes in simulation parameters affect the data, you can plot multiple series on an XY plot. Simulate the model again using a higher initial velocity for the ball.

Using the Simulink® Editor or the MATLAB® Command Window, change the Initial value parameter of the Initial Velocity block to 25. Then, simulate the model.

set_param(mdl+"/Initial Velocity","Value","25")
sim(mdl);

The Simulation Data Inspector moves the first run to the archive and transfers the view to the new run. To adjust the zoom level for the signals from the new run, click Fit to View Fit to View icon.

Drag the first run from the archive into the work area. Then, use the run actions menu to copy the plotted signal selection from the current run to the first run.

To copy the plotted signal selection from the current run, click the three dots on the right of the row for the current run. Then, under Plotted signal selection, select Copy.

The run actions menu with Copy selected

To plot data from the first run, paste the plotted signal selection onto the run. Click the three dots on the right of the row for the first run. Then, under Plotted signal selection, select Paste.

Signals from two runs plotted in the Simulation Data Inspector.

The signals in both runs have the same names and the same signal colors. Change the colors for the signals in the first run.

  1. Click the representation of the line in the table.

  2. Select a new color.

  3. Click Set.

The lines color and style menu

The signals in both runs also have the same name. To determine which run contains a plotted signal, you can use the tooltip in the legend.

The tooltip in the legend shows which run the signals are from.

You can also rename the signals. For example, rename the signals in the first run to Position-1 and Velocity - 1. To rename a signal, double-click the name in the table and enter a new name.

The signal table showing the signal names being edited

When you add multiple series to an XY plot, each series gets a cursor. All cursors on the XY plot move synchronously, so all signal values displayed on the cursors correspond to the same time.

The Simulation Data Inspector displays data from multiple runs, using distinct colors and signal names for each.

You can manage the signals plotted on an XY plot using the XY Data dialog box. Right-click the XY plot to access the subplot context menu. Then, select Show plotted signals. Using the XY Data dialog box, you can remove an x, y data pair from the plot or modify which signals provide the x-data and y-data for each data pair.

xy_manage_signals.png

See Also

Blocks

Tools

Topics