| Contents | Index |
| On this page… |
|---|
You can import the initial values of the states in a system (in other words, the initial conditions of the system), at the beginning of a simulation. You can then save the final values of the states at the end of the simulation. Using this approach of logging states saves a steady-state solution so that you can restart the simulation at that known state.
To save the model state at each simulation step, select Configuration Parameters > Data Import/Export > States (in the Save to workspace area) and enter a name in the adjacent edit field . Simulink saves the state in a workspace variable that has the name you specify. The default variable name is xout. The saved data has the format that you specify in the Data Import/Export > Save options area.
You can save the final model state instead of the state at each simulation step. Select Configuration Parameters > Data Import/Export > Final states. The default name of the final state variable is xFinal.
When Simulink saves states from a referenced model in the structure-with-time format, Simulink adds a Boolean subfield (named inReferencedModel) to the signals field of the saved data structure. The value of this additional field is true (1) if the signals field records the final state of a block that resides in the submodel, and a 0 otherwise. For example:
>> xout.signals(1)
ans =
values: [101x1 double]
dimensions: 1
label: 'DSTATE'
blockName: [1x66 char]
inReferencedModel: 1
If the model has no states saved, then xout is an empty variable. To determine whether a model has states saved, use the isempty(xout) command.
If the signals field records a submodel state, its blockName subfield contains a compound path of a top model path and a submodel path. The top model path is the path from the model root to the Model block that references the submodel. The submodel path is the path from the submodel root to the block whose state the signals field records. The compound path uses a | character to separate the top and submodel paths. For example:
>> xout.signals(1).blockName ans = sldemo_mdlref_basic/CounterA|sldemo_mdlref_counter/Previous Output
To load states, check Initial state in the Load from workspace area of the Data Import/Export pane and specify the name of a variable that contains the initial state values (for example, a variable containing states saved from a previous simulation). The initial values that the workspace variable specifies override the initial state values that the blocks in the model specify in initial condition parameters.
Use the structure or structure-with-time option to specify initial states to accomplish any of the following:
Associate initial state values directly with the full path name to the states. This association eliminates errors that can occur if Simulink reorders the states, but the order of the initial state array does not change correspondingly.
Assign a different data type to the initial value of each state.
Initialize only a subset of the states.
For example, the following commands create an initial state structure that initialize the x2 state of the vdp model. The x1 state is not initialized in the structure. Therefore, during simulation, Simulink uses the value in the Integrator block associated with the state.
% Open the vdp demo model vdp % Use getInitialState to obtain an initial state structure states = Simulink.BlockDiagram.getInitialState('vdp'); % Set the initial value of the signals structure element % associated with x2 to 2. states.signals(2).values = 2; % Remove the signals structure element associated with x1 states.signals(1) = [];
To use the states variable, for the vdp model, enable the Configuration Parameters > Data Import/Export > Initial state option (in the Load from workspace area). Enter states into the associated edit field. When you run the model, note that both states have the initial value of 2. The initial value of the x2 state is assigned in the states structure, while the initial value of the x1 state is assigned in its Integrator block.
Note Use the structure or structure-with-time format to initialize the states of a top model and the models that it references. |
![]() | Importing Data Structures to a Root-Level Input Port | Working with Data Stores | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |