Main Content

Save State Information

In addition to calculating an output value for each time step, some blocks also store information about the block state. Log state data to analyze how block states change throughout the simulation. Log model operating points during a simulation to resume the simulation from any of those operating points. You can enable States logging to analyze how block states change throughout a simulation. To log a snapshot of block states when a simulation is paused or stopped, you can configure a model to log Final states.

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.

Open the vdp model.

mdl = "vdp";
open_system(mdl)

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.

set_param(mdl,"SaveState","on")

Simulate the model.

out = sim(mdl);

By default, all logged simulation data is returned as a single Simulink.SimulationOutput object in a variable named out.

out
out = 
  Simulink.SimulationOutput:
                   tout: [64x1 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, state data is logged in Dataset format with the variable name xout.

states = out.xout
states = 
Simulink.SimulationData.Dataset 'xout' with 2 elements

                        Name  BlockPath 
                        ____  _________ 
    1  [1x1 State]      ''    vdp/x1   
    2  [1x1 State]      ''    vdp/x2   

  - Use braces { } to access, modify, or add elements using index.

Use curly braces to access the state data for the first element of the Simulink.SimulationData.Dataset object. State information is logged for each simulation step.

stx1 = states{1};
stx1.Values.Data
ans = 64×1

    2.0000
    2.0000
    2.0000
    2.0000
    1.9998
    1.9943
    1.9379
    1.8155
    1.5990
    1.2687
      ⋮

Save Final State Information

You can also save a snapshot of the block states only when a simulation is paused or stopped by logging final states.

To log state information at the end of the simulation, 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 switch to final states logging programmatically.

set_param(mdl,"SaveState","off")
set_param(mdl,"SaveFinalState","on")

Simulate the model.

out = sim(mdl);

Access the logged final state data using dot notation. By default, final state data is logged in Dataset format with the variable name xFinal.

fstates = out.xFinal
fstates = 
Simulink.SimulationData.Dataset 'xFinal' with 2 elements

                        Name  BlockPath 
                        ____  _________ 
    1  [1x1 State]      ''    vdp/x1   
    2  [1x1 State]      ''    vdp/x2   

  - Use braces { } to access, modify, or add elements using index.

Use curly braces to access the state data for the first element of the Simulink.SimulationData.Dataset object. Final state information is logged only for the last simulation step.

fstx1 = fstates{1};
fstx1.Values.Data
ans = 
2.0108

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 'vdp' at simulation time 20.

  Properties

    loggedStates
    description
    startTime (Read-only)
    snapshotTime (Read-only)

  Methods

    get
    set


See Also

Model Settings

Objects

Related Topics