MATLAB Examples


This script file will show the following:

  1. Remove dependance on Simulink state ordering for Simulink state initialization or state output processing, using existing Structure Format option which keys off of full state block path instead of array index.
  2. Shows one can initialize only a subset of Simulink states, no need to define complete initial state vector. Uses initial value within Simulink model if not defined within Structure.

Although this example does not do the following explicitly, this Structure Format approach also allows for - saving and setting continuous, discrete, differing datatype states all at the same time! - Defining Root Level Input time histories! Defining processing scripts of Root Level Output time histories, or State time histories!

Refer to the documentation for more information:

ADVICE: Did you know you can use the MATLAB debugger to step through this file line by line! WOW!


Clean up session

bdclose all
clear all

Use the VDP model

disp('bring up the VDP model')
mdl = 'vdp';
bring up the VDP model

Create the state structure.

The much more popular approach is to simply have the structure created by running the simulation and saving final state in the desired Format. This is done through the Workspace I/O tab.

This following function is provided as a utility to create this structure programatically if so desired.

xstruct = Simulink.BlockDiagram.getInitialState(mdl)

% Let's only set one of the initial states
% And pick the 'second' one as would be returned by the Array format!
% Notice this keys off of the unique Simulink state definition of complete
% block path containing the state.
xin = xstruct;
xin.signals(1) = [];
xin.signals.values = -.5;

% extract opts for this model
opts = simget(mdl);
opts.InitialState = xin;
opts.SaveFormat = 'StructureWithTime';


disp('Note the initial condition on the time trace in the scope!')
xstruct = 

       time: 0
    signals: [1x2 struct]

ans =


Note the initial condition on the time trace in the scope!


Experiment with the Format set to structure with time and saving the complete time history of the states. This is done through the Workspace I/O tab in the Simulation Parameters dialog.

Write a script that keys off of xout.signal.blockName, and plots state 'vdp/x2' vs time. DO NOT key off of index of the XOUT structure!!! That will defeat the whole purpose!

Do you see how this will allow for scripts that will function regardless of which states are added to the Simulink model? Your scripts will then depend on the model heirarchical location of the states instead, which means if those DO change then your scripts will need to as well! But you can leverage the automatic structure creation, as well as Simulink State definition from Example1 to automate and script both of these aspects.

Before you know it, your state initial conditions, state outputs, root output data, root level input data, will be completely robust to model additions/changes of any of these things!!! Whoo-Hoo!