Main Content

snapshot

Class: Simulink.sdi.CustomSnapshot
Package: Simulink.sdi

Create a custom snapshot

Syntax

fig = snap.snapshot
[fig,image] = snap.snapshot
fig = snap.snapshot(Name,Value)
[fig,image] = snap.snapshot

Description

fig = snap.snapshot creates a figure according to the properties of the Simulink.sdi.CustomSnapshot object, snap, and returns the handle for the figure, fig.

[fig,image] = snap.snapshot creates a figure according to the properties of the Simulink.sdi.CustomSnapshot object, snap, and returns the handle for the figure, fig, and an array of image data, image.

fig = snap.snapshot(Name,Value) creates a figure according to the properties of the Simulink.sdi.CustomSnapshot object, snap, with additional options specified by one or more Name,Value pair arguments. This syntax returns the figure handle, fig.

[fig,image] = snap.snapshot creates a figure according to the properties of the Simulink.sdi.CustomSnapshot object, snap, with additional options specified by one or more Name,Value pair arguments. This syntax returns the figure handle, fig, and an array of image data, image.

Input Arguments

expand all

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'to','figure','props',{'Name','My Data'}

Type of snapshot to create.

  • 'image' — Create a figure and return the figure handle and an array of image data. When you specify 'to','image', the fig and image outputs both have value.

  • 'figure' — Create a figure and return the figure handle. When you specify 'to','figure' the fig output has value, and the image output is empty.

  • 'file' — Save to a PNG file with the name specified by the filename name-value pair. If you do not specify a filename name-value pair, the file is named plots.png. When you specify 'to','file', the fig and image outputs are both empty.

  • 'clipboard' — Copy the plots to your system clipboard. From the clipboard, you can paste the image into another program such as Microsoft® Word. When you specify 'to','clipboard', the fig and image outputs are both empty.

Example: 'to','file'

Data Types: char | string

Name of the image file to store the snapshot when you specify 'to','file'.

Example: 'filename','MyImage.png'

Data Types: char | string

Figure properties, specified as a cell array. You can include settings for the figure properties described in Figure Properties.

Example: 'props',{'Name','MyData','NumberTitle','off'}

Data Types: char | string

Output Arguments

expand all

Handle for the figure. When a figure is not created with your specified options, the fig output is empty.

Array of image data. The image output has value when you use Simulink.sdi.snapshot without any input arguments or without a to name-value pair and when you specify 'to','image'.

Examples

expand all

This example shows how to copy view settings from one run to another and how to create figures using the Simulink.sdi.CustomSnapshot object.

Simulate Your Model and Get a Run Object

Configure the vdp model to save output data, and run a simulation to create data.

load_system('vdp')
set_param('vdp','SaveFormat','Dataset','SaveOutput','on')
set_param('vdp/Mu','Gain','1');
sim('vdp');

Use the Simulation Data Inspector programmatic interface to access the run data.

runIndex = Simulink.sdi.getRunCount;
runID = Simulink.sdi.getRunIDByIndex(runIndex);
vdpRun = Simulink.sdi.getRun(runID);

Modify Signal View Settings

Use the Simulink.sdi.Run object to access signals in the run. Then, modify the signal view settings. This example specifies the line color and style for each signal. The view settings for the run comprise the view settings for each signal and view settings specified for the plot area.

sig1 = vdpRun.getSignalByIndex(1);
sig2 = vdpRun.getSignalByIndex(2);

sig1.LineColor = [0 0 1];
sig1.LineDashed = '-.';

sig2.LineColor = [1 0 0];
sig2.LineDashed = ':';

Capture a Snapshot from the Simulation Data Inspector

Create a Simulink.sdi.CustomSnapshot object and use the Simulink.sdi.snapshot function to programmatically capture a snapshot of the contents of the Simulation Data Inspector.

snap = Simulink.sdi.CustomSnapshot;

You can use properties of the Simulink.sdi.CustomSnapshot object to configure the plot settings, like the subplot layout and axis limits, and to plot signals. When you use a Simulink.sdi.CustomSnapshot object to create your figure, these plot settings do not affect the Simulation Data Inspector.

snap.Rows = 2;
snap.YRange = {[-2.25 2.25],[-3 3]};
snap.plotOnSubPlot(1,1,sig1,true)
snap.plotOnSubPlot(2,1,sig2,true)

Use Simulink.sdi.snapshot to generate the figure you specified in the properties of the Simulink.sdi.CustomSnapshot object.

fig = Simulink.sdi.snapshot("from","custom","to","figure","settings",snap);

Copy the View Settings to a New Simulation Run

Simulate the model again, with a different Mu value. Use the Simulation Data Inspector programmatic interface to access the simulation data.

set_param('vdp/Mu','Gain','5')
sim('vdp');

runIndex2 = Simulink.sdi.getRunCount;
runID2 = Simulink.sdi.getRunIDByIndex(runIndex2);
run2 = Simulink.sdi.getRun(runID2);

To create a plot of the new output data that looks like the one you created in the previous step, you can copy the view settings to the run in a single line of code using Simulink.sdi.copyRunViewSettings. The Simulink.sdi.copyRunViewSettings function does not automatically update plot settings in Simulink.sdi.CustomSnapshot objects, so specify the plot input as false.

sigIDs = Simulink.sdi.copyRunViewSettings(runID,runID2,false);

Capture a Snapshot of the New Simulation Run

Use the Simulink.sdi.CustomSnapshot object to capture a snapshot of the new simulation run. First, clear the signals from the subplots. Then, plot the signals from the new run and capture another snapshot.

snap.clearSignals
snap.YRange = {[-2.25 2.25],[-8 8]};
snap.plotOnSubPlot(1,1,sigIDs(1),true)
snap.plotOnSubPlot(2,1,sigIDs(2),true)

fig = snap.snapshot("to","figure");

Introduced in R2018a