Working with Simulink.SimulationOutput Objects in MATLAB
Simulink.SimulationOutput provides a unified interface for accessing simulation results and associated metadata.
Overview
The Simulink.SimulationOutput object serves as a container for all data generated during a simulation run. Each property within the object corresponds to logged data from the simulation, while additional properties contain metadata about the model configuration, simulation timing, and any errors or warnings that occurred during execution. This centralized access pattern helps separate simulation results from other workspace variables and simplifies managing data from multiple simulation runs.
Object Creation
A Simulink.SimulationOutput object is automatically created when:
-
The Single Simulation Output parameter is enabled
This parameter is enabled by default for new models. Enable it via the Configuration Parameters dialog: go to the Modeling tab, click Model Settings under the Settings section, then select Data Import/Export and check Single Simulation Output.
-
Running multiple simulations using the Simulation Manager
-
Simulating with one or more Simulink.SimulationInput objects programmatically
SimulationInput objects configure simulation parameters when using the
sim,parsim, orbatchsimfunctions. -
Using the
simfunction syntax that returns results as a single output object
Properties
Logged Data Properties
For each logged variable created during simulation, the Simulink.SimulationOutput object contains a corresponding property. The property name matches the variable name specified during logging configuration. For instence, enabling signal logging with the default variable name logsout creates a property containing the logged signal data.
Configure logged data and variable names through the Configuration Parameters dialog's Data Import/Export pane, or by adding logging blocks such as the To Workspace block to the model.
Custom Properties
Data logged to files using To File blocks, Record blocks, or the Dataset logging to file parameter are not captured as Simulink.SimulationOutput properties.
You can add custom properties to store additional data or metadata. For example, when running parallel simulations with parsim or batchsim, custom properties enable transferring data from parallel workers back to the client.
Adding a custom property follows the same pattern as defining a field in a struct:
simOut.CustomProperty = 42;
Examples
Accessing Logged Data
When simulating a model with single output enabled, use the Simulink.SimulationOutput object to retrieve all logged data and metadata.
This example model logs data using multiple methods:
- Signal logging captures the Sine Wave block output
- To Workspace block captures the Gain block output
- Record blocks capture Gain, Chirp Signal, and Square Wave Generator outputs
- Output port logging captures the Square Wave Generator output
The model also logs time data.
Open the model:
mdl = "LoggingBlocks";
open_system(mdl)
Create a Simulink.SimulationInput object to configure simulation, setting the stop time to 25:
simIn = Simulink.SimulationInput(mdl);
simIn = setModelParameter(simIn, 'StopTime', '25');
Run the simulation. The sim function returns result as a Simulink.SimulationOutput object containing all logged data. Each logging method's data appears as a property matching the configured variable name.
result = sim(simIn);
Access logged data using dot notation, the get function, or the find function.
Access the Big Sine signal logged via To Workspace:
wsData = result.simout
wsData =
timeseries
Common Properties:
Name: 'Big Sine'
Time: [51x1 double]
TimeInfo: tsdata.timemetadata
Data: [51x1 double]
DataInfo: tsdata.datametadata
Access the Sine signal logged via signal logging:
sigData = get(result, "logsout")
sigData =
Simulink.SimulationData.Dataset 'logsout' with 1 element
Name BlockPath
____ _______________________
1 [1x1 Signal] Sine LoggingBlocks/Sine Wave
- Use braces { } to access, modify, or add elements using index.
Access the Square Wave signal logged via output port logging:
portData = find(result, "yout")
portData =
Simulink.SimulationData.Dataset 'yout' with 1 element
Name BlockPath
___________ ___________________
1 [1x1 Signal] Square Wave LoggingBlocks/Outport
- Use braces { } to access, modify, or add elements using index.
Access simulation metadata using dot notation or getSimulationMetadata:
runMeta = getSimulationMetadata(result)
runMeta =
SimulationMetadata with properties:
ModelInfo: [1x1 struct]
TimingInfo: [1x1 struct]
ExecutionInfo: [1x1 struct]
UserString: ''
UserData: []
The metadata returns as a Simulink.SimulationMetadata object, organizing simulation information into struct-based properties while providing string and data fields for custom information.
Access the ExecutionInfo property. The execution summary shows the simulation reached its stop time at 25 with no warnings or errors:
runMeta.ExecutionInfo
ans = struct with fields:
StopEvent: 'ReachedStopTime'
StopEventSource: []
StopEventDescription: 'Reached stop time of 25'
ErrorDiagnostic: []
WarningDiagnostics: [0x1 struct]
Modifying Object Contents
Simulink.SimulationOutput represents simulation results by combining metadata with all logged data. Modify contents by adding or removing logged variables and custom properties.
Open model LoggingBlocks, which logs multiple input signals using various methods:
- Signal logging captures the Sine Wave output
- To Workspace captures the Gain output
- Record blocks capture Gain, Chirp Signal, and Square Wave Generator outputs
- Output port logging captures the Square Wave Generator output
The model also logs time data.
mdl = "LoggingBlocks";
open_system(mdl);
Save the Sine Wave amplitude and frequency parameters using get_param. Store the values in a struct:
waveConfig.waveAmplitude = get_param(strcat(mdl, "/Sine Wave"), "Amplitude");
waveConfig.waveFrequency = get_param(strcat(mdl, "/Sine Wave"), "Frequency");
Run the simulation:
simResult = sim(mdl);
The result contains all logged variables created during simulation. Use who to list modifiable properties:
propList = who(simResult)
propList = 5x1 cell
{'logsout' }
{'recordout'}
{'simout' }
{'tout' }
{'yout' }
For this simulation, assume you only need data related to the Sine Wave signal. Remove the recordout and yout properties:
simResult = removeProperty(simResult, ["recordout", "yout"]);
who(simResult)
This Simulink.SimulationOutput object contains these editable properties:
logsout simout tout
Alternatively, add data using dot notation or the setUserData function to set the UserData property in the SimulationMetadata object.
Save the Sine Wave parameters as a custom property using dot notation:
simResult.WaveParameters = waveConfig;
who(simResult)
This Simulink.SimulationOutput object contains these editable properties:
WaveParameters logsout simout tout
Displaying Errors in Diagnostic Viewer
Use sldiagviewer.reportSimulationMetadataDiagnostics to display errors and warnings captured in the Simulink.SimulationOutput object via the Diagnostic Viewer.
Open model ex_sldemo_bounce:
model = "ex_sldemo_bounce";
open_system(model)
Introduce an error by setting the Initial Velocity block value to an undefined variable z:
set_param("ex_sldemo_bounce/Initial Velocity", "Value", "z");
Create a Simulink.SimulationInput object to configure the simulation:
configObj = Simulink.SimulationInput(model);
Run the simulation. With StopOnError set to "off", errors and warnings are captured in the SimulationOutput object rather than reported in the command window or interrupting script execution:
outputObj = sim(configObj, "StopOnError", "off", "ShowProgress", "off");
Warning: One or more simulations completed with errors. For more information, inspect the SimulationOutput objects at these indices:
[1]
Display the simulation warnings and errors in the Diagnostic Viewer:
sldiagviewer.reportSimulationMetadataDiagnostics(outputObj)