Save State Information
R2026bIn addition to calculating an output value for each time step, some blocks also store information about the block state.
To analyze how block states change throughout a simulation, enable States logging.
To log a snapshot of block states when a simulation is paused or stopped, log Final states.
To save a complete model operating point to use as the initial state of another simulation, log final states and Save final operating point.
Save State Information for Each Simulation Step
You can save state information for logged states for each simulation step during a simulation. Saving state information for each simulation step can be helpful for debugging.
To enable States logging, in the Configuration Parameters dialog box, in the Data Import/Export pane, select the States parameter.
Alternatively, you can enable states logging programmatically.
mdl = "IntegrateSine"; open_system(mdl) set_param(mdl,"SaveState","on")
Simulate the model. By default, the software returns logged simulation data as a single Simulink.SimulationOutput object in a variable named out.
out = sim(mdl)
out =
Simulink.SimulationOutput:
tout: [51x1 double]
xout: [1x1 Simulink.SimulationData.Dataset]
yout: [1x1 Simulink.SimulationData.Dataset]
SimulationMetadata: [1x1 Simulink.SimulationMetadata]
ErrorMessage: [0x0 char]
Access the logged state data using dot notation. By default, the software logs state data in Dataset format with the variable name xout.
states = out.xout
states =
Simulink.SimulationData.Dataset 'xout' with 1 element
Name BlockPath
____ ________________________
1 [1x1 State] '' IntegrateSine/Integrator
- Use braces { } to access, modify, or add elements using index.
To access the state data by index, use curly braces. State information is logged for each simulation step. For example, get the last five logged state values.
stx1 = states{1};
stx1.Values.Data(end-4:end)ans = 5×1
1.9748
1.9997
1.9847
1.9304
1.8391
Save Final State Information
If you do not need state data at every simulation step, save a snapshot of the block states only when a simulation is paused or stopped by logging final states.
To log only final state information, stop logging state information at each time step and enable Final states logging. In the Configuration Parameters dialog box, in the Data Import/Export pane, clear the States parameter. Then, select the Final states parameter.
Alternatively, you can log only final states programmatically.
set_param(mdl,"SaveState","off") set_param(mdl,"SaveFinalState","on")
Simulate the model again. This time, the software saves only the state data for the final simulation step.
out = sim(mdl);
By default, the software saves final state data in Dataset format with the variable name xFinal. Access the logged final state data using dot notation.
fstates = out.xFinal
fstates =
Simulink.SimulationData.Dataset 'xFinal' with 1 element
Name BlockPath
____ ________________________
1 [1x1 State] '' IntegrateSine/Integrator
- Use braces { } to access, modify, or add elements using index.
To access the state data, use curly braces to get the first element in the Simulink.SimulationData.Dataset object. The software logs final state information only for the last simulation step.
fstx1 = fstates{1};
fstx1.Values.Dataans = 1.8391
Save Complete Model Operating Point
When you want to use final states data as the initial state for another simulation, consider saving the complete model operating point. When you choose to save the complete model operating point, the software saves hidden block states, the state of the solver and execution engine, and some block output values in addition to final block states.
In the Configuration Parameters dialog box, in the Data Import/Export pane, check that the Final states parameter is selected. Then, select Save final operating point.
Alternatively, you can save the complete model operating point programmatically.
set_param(mdl,"SaveFinalState","on","SaveOperatingPoint","on")
Simulate the model.
out = sim(mdl);
Use dot notation to access the complete model operating point using the default final state variable name xFinal.
out.xFinal
ans =
Simulink.op.ModelOperatingPoint
Operating point of the model 'IntegrateSine' at simulation time 10.
Properties
loggedStates
description
startTime (Read-only)
snapshotTime (Read-only)
Methods
get
set