Products & Services Solutions Academia Support User Community Company

Learn more about Simulink   

Importing and Exporting States

Introduction

You can import the initial values of a system's states, i.e., its initial conditions, at the beginning of a simulation and save the final values of the states at the end of the simulation. This feature allows you to save a steady-state solution and restart the simulation at that known state.

Saving Final States

To save the final values of a model's states, select Final states in the Save to workspace area of the Data Import/Export pane and enter a name in the adjacent edit field. The states are saved in a workspace variable having the specified name. The saved data has the format that you specify in the Save options area of the Data Import/Export pane.

When saving states from a referenced model in the structure-with-time format, a boolean subfield is added named inReferencedModel to the signals field of the saved data structure. This field's value 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 signals field records a submodel state, its blockName subfield contains a compound path comprising 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, e.g.,

>> xout.signals(1).blockName

ans =

sldemo_mdlref_basic/CounterA|sldemo_mdlref_counter/Previous Output

Loading Initial States

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 specified by the workspace variable override the initial values specified by the model itself, i.e., the values specified by the initial condition parameters of those blocks in the model that have states.

Use the structure or structure-with-time option to specify initial states if you want to accomplish any of the following.

For example, the following commands create an initial state structure that can be used to initialize the x2 state of the vdp model. The x1 state is not initialized in the structure and, therefore, the value entered into the state's associated Integrator block is used during the simulation.

% 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 this states variable, open the Configuration Parameters dialog box for the vdp model. Check Initial state in the Load from workspace area of the Data Import/Export pane and type 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.

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS