Main Content

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 for each simulation step using the States configuration parameter. 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 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 only includes 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, use the Configuration Parameters dialog box. On the Modeling tab, click Model Settings, then select Data Import/Export.

Model Configuration ParameterType of State Information SavedWhen State Information Is SavedHow State Information Is SavedDefault Variable Name
States

Block states

Each simulation time step

In format specified using Format parameter

xout
Final states

Block states

When simulation is paused or stopped

In format specified using Format parameter

xFinal
Final states and Save final operating point

Complete simulation state for the model, including:

  • Block states

  • Hidden states that are not captured by states logging, including states for:

    • Transport Delay blocks

    • Variable Transport Delay blocks

    • From Workspace blocks

    • For Each Subsystem blocks

    • Conditionally executed subsystems

    • Stateflow® charts

    • MATLAB® System blocks

    • S-functions that implement custom operating point methods

    • Simscape™ Multibody™ second-generation blocks

  • Solver and execution engine states

  • Zero-crossing information

  • Output values for blocks where the output value is needed to restore the simulation state

When simulation is paused or stopped

As a Simulink.op.ModelOperatingPoint object

The loggedStates property of the ModelOperatingPoint object stores the block states in the format specified by the Format parameter.

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 WorkflowFinal States Without Operating PointFinal States with Operating Point

Faster simulation workflow

Final states without operating point are not always sufficient to resume 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 simulate a model from a particular state.

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:

  • The Start time for the simulation matches the Start time for the simulation that produced the operating point.

  • The simulation computes results starting from the time at which the operating point was created, saved in the snapshotTime property of the ModelOperatingPoint object.

For more information, see Use Model Operating Point for Faster Simulation Workflow.

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 snapshotTime property for the Simulink.op.ModelOperatingPoint object.

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, xFinal by default.

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 loggedStates property for the Simulink.op.ModelOperatingPoint object

The ModelOperatingPoint object contains data for more states than when you log states without the operating point. Some of these additional states that you can view using the loggedStates property are read-only.

Partial state restore

Can use subset of saved state information to specify initial state for simulation.

Must use complete operating point as initial state for simulation.

Specifying initial states that include bus data

Supported for final states saved using the Dataset format

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 use saved operating point as initial state for simulation when you do not modify the model and after making certain changes, including:

  • Changing tunable parameter values.

  • Modifying logging or visualization options.

  • Modifying solver settings, as long as the type of solver remains the same (fixed-step or variable-step).

For details, see Model Changes Between Saving and Restoring Model Operating Points.

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 Additional Considerations for Saving and Restoring Model Operating Points.

Code generation

Supported for state information saved using a format other than the Dataset formatNot supported

Choose Format for Logged State Information

You can specify the format used to log state information using the 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 Format parameter specifies the format for the logged data. When you save final states with the model operating point, the Format parameter specifies the format for the data in the loggedStates property of the Simulink.op.ModelOperatingPoint object. The 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 WorkflowDatasetStructure and Structure with time

Logging multiple sample values for a given time step

Supported

Can be important for logging Stateflow data or data inside a For Iterator Subsystem or While Iterator Subsystem.

Not supported

Streaming data to the Simulation Data Inspector

Supported

Not supported

Rapid accelerator simulation

Logging states throughout simulation is not supported.

Logging final states is supported.

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

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.

See Also

Model Settings

Objects

Related Topics