Load State Information
Import Initial States
To initialize a simulation, you can use:
Final state information (with or without
ModelOperatingPoint
object) from a previous simulationState information that you create in MATLAB®
Use Configuration Parameters > Data Import/Export parameters to import initial states.
Enable the Initial state parameter.
In the Initial state edit box, enter the name of the variable for the state information that you want to use for initialization.
The initial values that the variable specifies override the initial state values that the blocks in the model specify in initial condition parameters.
You can specify Dataset
, structure, or structure with time
data.
Initialize a State
You can initialize a specific state. This example creates an initial state structure for
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
x1
state.
Open the model.
open_system('vdp');
Set the
SaveFormat
model argument to'Structure'
.set_param('vdp','SaveFormat','Structure');
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) = [];
Use the
states
variable for thevdp
model. Select the initial state configuration parameter.set_param('vdp','LoadInitialState','on','InitialState','states');
Simulate the model and examine the initial values of
x2
andx1
.sim('vdp'); states
states = struct with fields: time: 0 signals: [1×1 struct]
states.signals
ans = struct with fields: values: 2 dimensions: 1 label: 'CSTATE' blockName: 'vdp/x2' stateName: '' inReferencedModel: 0 sampleTime: [0 0]
When you simulate the model, 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.
Initialize States in Referenced Models
To initialize the states of a top model and the models that it references, use the structure or structure with time format or use operating point.
If the top model is in rapid accelerator mode, you cannot load discrete state data.