Main Content

Visualize Simulation Data on an XY Plot

When you log simulation data in a model, you can view the simulation results on an XY plot in the Simulation Data Inspector. Then, you can replay the simulation to animate the relationships between the signals in your model. This example simulates a model and plots data logged in the simulation using time plots and XY plots in the Simulation Data Inspector. The example also shows how to inspect the plotted data using replay controls and cursors. The XY plot used in this example is also available in the Record and XY Graph blocks.

Simulate the Model and Open the Simulation Data Inspector

The model in this example simulates the dynamics of a bouncing ball, logging the velocity and position of the ball as outputs.


Because the model is configured to log output data, the 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.


Plot Data on an XY Plot

To plot the data on an XY plot, you need to add the visualization to the layout. By default, the Simulation Data Inspector uses time plots for each subplot in the layout.

To add an XY plot to your layout, open the Layout menu and click Edit View to open the Visualization Gallery.

From the Visualization Gallery, drag and drop the XY icon onto the plot.

To plot the signals on the XY plot, select both signals then drag and drop them onto the plot. You can specify which signal to use as the x data and which to use as the y data. For this example, use Position as the x data and Velocity for the y data.

You can modify the appearance of the XY plot by opening the Visualization Settings. The line and marker colors match the color of the signal that provides the x data or the signal that provides the y data. By default, the line uses the y-axis signal color.

Add Time Plots and Inspect the Data

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

In the Layout menu, from the Basic Layouts section, select the layout with two subplots on top of a third. Then, plot the Position signal in the upper-right time plot, and plot the Velocity signal on the bottom time plot.

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 corresponding to the point is displayed in the upper-right of the plot.

Move the cursor in the XY plot along the plotted line. As you move the cursor, the next data point for the cursor to snap to is highlighted. 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.

When you drag a 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 the 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.

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.

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. Using the Simulink® Editor or the MATLAB™ Command Window, change the Initial value parameter of the Initial Velocity block to 25 and simulate the model.

set_param('ex_sldemo_bounce/Initial Velocity','Value','25')

The Simulation Data Inspector moves the first run to the archive and transfers the view to the new run. Drag the first run from the archive into the work area.

The signals in both runs have the same names and the same signal colors. Before you add data from the first run to the plots, change the signal names and colors. For example, you can rename the signals from the first run Position-1 and Velocity-1. To modify a signal color, click the line representation in the table, select a new color, and click Set. To modify the signal names, double-click the name in the table and enter the new name.

Now, add the data from the first run to the plots in the layout. Plot the signals from the first run on the time plots. To add another series to the XY plot, select both signals and drag them onto the XY plot. The series menu shows the first series that is already plotted. Select the Position-1 signal as the x-data for series 2 and Velocity-1 as the series 2 y-data. Then, click OK.

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.

You can manage the signals plotted on an XY plot using the subplot context menu. Right-click the XY plot and select Show plotted signals. From the series menu, you can remove series from the plot or modify which signals provide the x-data and y-data for each series.

See Also



Related Topics