Save Block States and Simulation Operating Points
During simulation, blocks in a model calculate an output value for each time step using the simulation time and inputs to the block. Some blocks also store information about their state and use that state information along with the block inputs and simulation time to calculate the output value for each time step. For example, the Unit Delay block stores the output signal value from the previous time step as its state and uses that information to calculate the output value for the current time step.
In addition to block states, other aspects of the model have state information that affects the simulation. The model operating point represents the complete set of block states and the state of the model, including the simulation time, the solver state, and the execution engine state.
Using logged block states, final states, and operating points, you can:
Analyze changes in block states throughout a simulation.
Specify initial states for blocks in the model.
Run a set of simulations more quickly by simulating from an initial operating point.
Choose Which State Information to Log
Depending on how you want to use the state information, you can configure your model to:
Log block states for every simulation time step.
Log a snapshot of block states when the simulation is paused or when the simulation stops.
Log the complete model operating point when the simulation is paused or when the simulation stops.
To analyze how block states change throughout simulation, log block states at each time step. To save a snapshot of the model for a given simulation time, log final states, with or without the complete model operating point.
You can log both states and final states in the same simulation. When you log final
states, you choose whether to save a snapshot of the simulation state that includes only
block states or a snapshot that captures the complete model operating point. When the model
is configured to return a single simulation output, logged states and final states are
returned as part of the Simulink.SimulationOutput
object.
To configure states logging for a model, open the Configuration Parameters dialog box. In the Simulink® Toolstrip, on the Modeling tab, click Model Settings. Then, select Data Import/Export.
Model Configuration Parameter | Type of State Information Saved | When State Information Is Saved | How State Information Is Saved | Default Variable Name |
---|---|---|---|---|
States | Block states | Each simulation time step | In format specified using Save format parameter | xout |
Final states | Block states | When simulation is paused or stopped | In format specified using Save format parameter | xFinal |
Final states and Save final operating point | Complete simulation state for the model, including:
| When simulation is paused or stopped | As a The
| xFinal |
Not all modeling patterns and workflows provide complete support for logging states and final states. Support for logging block states throughout the simulation depends on the format you use. For more information, see Choose Format for Logged State Information.
Support for saving final states depends on whether you save the model operating point and the format you use. When deciding how to save final states, consider your modeling requirements and how you want to use the final state.
Modeling Pattern or Workflow | Final States Without Operating Point | Final States with Operating Point |
---|---|---|
Simulation continuation workflow | Final states without operating point are not always sufficient to continue a simulation from a particular state and time without changing computed results. If you are unsure of the requirements for your model, use an operating point to specify initial states for simulation. | Use model operating points to save time by not repeatedly running segments of a simulation that do not change from one scenario to the next. Results from a simulation that starts from an operating point match exactly the results from an uninterrupted simulation that runs from the start. When you use an operating point as the initial state:
For more information, see Speed Up Simulation Workflows by Using Model Operating Points. |
Restore simulation time | Simulations that use final state as initial state start from the start time you specify using the Start time configuration parameter for the model. | Simulations that use operating point as initial state start from the
simulation time at which the operating point was saved, stored in the
|
Inspecting and modifying state values | Can modify values for individual states between saving final states and using the state information as the initial state for another simulation. View and edit the contents of the final states variable,
| Can modify values for individual states between saving operating point and using it as the initial state for another simulation. View and
edit the contents of the The
|
Partial state restore | Can use subset of saved state information to specify initial state for simulation. | Can configure operating point initialization to suit workflow requirements. For details, see Configure Flexibility of Operating Point Initialization. |
Specifying initial states that include bus data | Supported for final states saved using the
| Supported for all formats. |
Modifying model between simulations | Can use saved state information as initial state for simulation after modifying the model as long as the blocks that correspond to the states in the initial state exist in the model. | Can configure operating point initialization to suit workflow requirements. For details, see Configure Flexibility of Operating Point Initialization. |
Simulation mode | Supported for all simulation modes. | Supported for all simulation modes You can only use the model operating point as the initial state for a simulation run in the same simulation mode as the simulation that generated the operating point. |
Model reference | Supported. | Limitations apply when you configure referenced models to simulate in accelerator mode. For details, see Model Operating Point Considerations and Limitations. |
Code generation | Supported for state information saved using a format other than the
Dataset format. | Not supported. |
Choose Format for Logged State Information
You can specify the format used to log state information using the Save
format parameter on the Data Import/Export pane of the
Model Configuration Parameters dialog box. When you log block states for each simulation
step or final states without the model operating point, the Save format
parameter specifies the format for the logged data. When you save final states with the
model operating point, the Save format parameter specifies the format
for the data in the loggedStates
property of the
Simulink.op.ModelOperatingPoint
object. The Save
format parameter also specifies the format for logged output data.
By default, states are logged using the Dataset
format. Block
states are returned in a Simulink.SimulationData.Dataset
object that
contains a Simulink.SimulationData.State
object for each
state. Logging states and outputs using the Dataset
format can
simplify postprocessing when you also use signal logging or log data stores, which are
always logged using the Dataset
format.
You can also use the Structure
or Structure with
time
format. The Dataset
,
Structure
, and Structure with time
formats store similar metadata, including the block path, along with the numeric state data.
The formats provide different levels of support for certain modeling patterns and
workflows.
Modeling Pattern or Workflow | Dataset | Structure and Structure with
time |
---|---|---|
Logging multiple sample values for a given time step | Supported Can be important for logging Stateflow data or data inside for-each, for-iterator, and while-iterator subsystems. | Not supported |
Streaming data to the Simulation Data Inspector | Supported | Not supported |
Specifying initial states that include bus data | Supported for final states saved with and without operating point | Supported for final operating point Not supported for final states saved without operating point |
Logging states inside function-call subsystems | Not supported | Supported |
Simulating referenced models in accelerator mode | Logging states throughout simulation is not supported. Logging final states is supported except when one or more states of the referenced model use any of these data types:
| Supported |
Rapid accelerator simulations and simulations deployed using Simulink Compiler™ | Logging states throughout simulation is not supported. Logging final states is supported except when one or more states use any of these data types:
| Supported |
Code generation | Not supported | Supported |
Logging states using the Array
format is not recommended. The
Array
format does not include metadata. You use the order of
the data in the array to determine which state value corresponds to which block in your
model. The arrangement of block states in the array is based on the sorted order for blocks
in the model, which can change from one simulation to the next, including when you modify
the model or change the simulation or code generation mode.