Main Content

Save State Information

Save State Information for Each Simulation Step

You can save state information for logged states for each simulation step during a simulation. That level of state information can be helpful for debugging.

  1. Select the Configuration Parameters > Data Import/Export > States check box.

  2. In the States edit box, you can specify a different variable for the state information, if you do not want to use the default xout variable.

  3. Also in the Data Import/Export pane, set the Format parameter to Dataset, Structure, or Structure with time, unless you use array format for compatibility with a legacy model.

    Dataset format does not support:

    • Logging states information inside a function-call subsystem

    • Logging states during rapid accelerator simulations

    • Code generation

  4. Click Apply.

  5. Simulate the model.

Save Partial Final State Information

To save just the logged states (the continuous and discrete states of blocks):

  1. Select the Configuration Parameters > Data Import/Export > Final states check box.

  2. In the Final states edit box, you can specify a different variable for the state information, if you do not want to use the default xFinal variable.

  3. Clear the Save final operating point parameter.

  4. Set the Format parameter to Dataset, Structure, or Structure with time.

  5. Click Apply.

  6. Simulate the model.

Examine State Information Saved Without the Operating Point

If you enable the Configuration Parameters > Data Import/Export > Final states or States parameters, Simulink® saves the state information in the format that you specify with the Format parameter. The default variable for Final state information is xFinal, and the variable for state information for States information is xout.

If a model has no states saved, then xFinal and xout are empty variables. To determine whether a model has states saved, use the isempty(xout) command.

Final State Information in Dataset Format

For example, suppose that you saved final state information in Dataset format, and use the default xFinal variable for the saved state information.

xFinal
xFinal = 

Simulink.SimulationData.Dataset 'xFinal' with 2 elements

                        Name    BlockPath 
                        ______  _________ 
    1  [1x1 State]      CSTATE  vdp/x1   
    2  [1x1 State]      DSTATE  vdp/x2   

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

Examine the first element of the state data set.

xFinal{1}

ans = 

  Simulink.SimulationData.State
  Package: Simulink.SimulationData

  Properties:
         Name: 'CSTATE'
    BlockPath: [1x1 Simulink.SimulationData.BlockPath]
        Label: CSTATE
       Values: [1x1 timeseries]

Final State Information in Structure with Time Format

For example, suppose that you saved final state information in a structure with time format, and use the default xFinal variable for the saved state information.

To find the simulation time and number of states in the vdp model, enter the xFinal variable.

xFinal
xFinal = 

       time: 20
    signals: [1x2 struct]

In this case, the simulation time is 20 and there are two states. To examine the first state, use this command.

xFinal.signals(1)
ans = 

               values: 2.0108
           dimensions: 1
                label: 'CSTATE'
            blockName: 'vdp/x1'
            stateName: ''
    inReferencedModel: 0

The values and blockName fields of the first state structure show that the final value for the output signal of the x1 block is 2.018.

Note

If you write a script to analyze state information, use a combination of label and blockName values to identify a specific state uniquely. Do not rely on the order of the states.

Save Final State Information with Operating Point

To save complete state information, save the ModelOperatingPoint object for a simulation.

  1. Select the Configuration Parameters > Data Import/Export > Final states check box.

  2. Also in the Data Import/Export pane, select the Save final operating point parameter.

  3. In the edit box next to the Save final operating point parameter, enter a variable name for the ModelOperatingPoint object and click Apply.

  4. Simulate the model.

For more information about using the operating point, see Use Model Operating Point for Faster Simulation Workflow.

See Also

Model Settings

Objects

Related Topics