Create Stateflow Charts for Execution as MATLAB Objects

To combine the advantages of state machine programming with the full functionality of MATLAB®, create a standalone Stateflow® chart outside of a Simulink® model. Save the standalone chart with the extension .sfx and execute it as a MATLAB object. Refine your design by using chart animation and graphical debugging tools.

With standalone charts, you can create MATLAB applications such as:

These applications can be shared and executed without requiring a Stateflow license. For more information, see Share Standalone Charts.

Construct a Standalone Chart

To construct a standalone Stateflow chart, open the Stateflow editor by using the edit function. For example, at the MATLAB Command Window, enter:

edit chart.sfx
If the file chart.sfx does not exist, the Stateflow editor opens an empty chart with the name chart. Otherwise, the editor opens the chart defined by the sfx file.

After you save a standalone chart, the help function displays information about executing it in MATLAB:

help chart.sfx

Create a Stateflow Chart Object

To execute a standalone chart in MATLAB, first create a Stateflow chart object. Use the name of the sfx file for the standalone chart as a function. Specify the initial values of data as name-value pairs. For example, this command creates the chart object chartObject, initializes data1 and data2, and executes its default transition:

chartObject = chart('data1',value1,'data2',value2)

To display chart information, such as the syntax for execution, the values of the chart data, and the list of active states, use the disp function:


Execute a Standalone Chart

After you define a Stateflow chart object, you can execute the standalone chart by calling the step function (with data values, if necessary):


Alternatively, you can call one of the input event functions:


If your chart has graphical or MATLAB functions, you can call them directly in the MATLAB Command Window. Calling a chart function does not execute the standalone chart.


You can execute a standalone chart without opening the Stateflow Editor. If the chart is open, then the Stateflow Editor highlights active states and transitions through chart animation.

For the purposes of debugging and unit testing, you can execute a standalone chart directly from the Stateflow editor. During execution, you enter data values and broadcast events from the user interface. For more information, see Execute and Unit Test Stateflow Chart Objects.

You can execute a standalone chart from a MATLAB script, a Simulink model, or an App Designer user interface. For more information, see:

Share Standalone Charts

You can share standalone charts with collaborators who do not have a Stateflow license.

If your collaborators have Stateflow, they can open, edit, and execute your standalone charts. During execution, chart animation and debugging is supported. Run-time error messages contain hyperlinks to the state or transition in the chart where the error occurs.

If your collaborators do not have Stateflow, they can execute your standalone charts as MATLAB objects without opening the Stateflow editor. Chart animation and debugging is not supported. Run-time error messages do not contain hyperlinks to the chart.

Properties and Functions of Stateflow Chart Objects

A Stateflow chart object encapsulates data and operations in a single structure by providing:

  • Private properties that contain the internal state variables for the standalone chart.

  • A step function that calls the various operations implementing the chart semantics.

A chart object can have other properties and functions that correspond to the various elements present in the chart.

Standalone Chart ElementsChart Object Elements
Local and constant dataPublic properties
Input eventsFunctions that execute the chart
Graphical and MATLAB functionsFunctions that you can call from the MATLAB Command Window

Chart Object Configuration Options

When you create a chart object, you can specify chart behavior by including these configuration options as name-value pairs.

Configuration OptionDescriptionExample
-animationDelaySpecify the delay that the chart animation uses to highlight each transition segment. The default value is 0.01 seconds. To produce chart animation with no delays, set to zero.

Create a chart object that has very slow animation by specifying one second delays:

chartObject = chart('-animationDelay',1)

-enableAnimationEnable chart animation. The default value is true.

Create a chart object that has animation disabled:

chartObject = chart('-enableAnimation',false)

-executeInitStepEnable the initial execution of default transitions. The default value is true.

Create a chart object but do not execute the default transition:

chartObject = chart('-executeInitStep',false)

-warningOnUninitializedDataEnable the warning about empty chart data after initializing the chart object. The default value is true.

Eliminate the warning when creating a chart object:

chartObject = chart('-warningOnUninitializedData',false)

Initialization of Chart Data

In the Stateflow Editor, you can use the Symbols window to specify initial values for chart data. When you create a chart object, chart data is initialized in alphabetical order according to its scope. Constant data is initialized first. Local data is initialized last.

If you use an expression to specify an initial value, then the chart attempts to resolve the expression by:

  • Using the values of other data in the chart.

  • Calling functions on the search path.

For example, suppose that you specify an initial value for the local data x by using the expression y. Then:

  • If the chart has a constant called y, y is initialized before x. The local data x is assigned the same initial value as y.

  • If the chart has a local data called y, x is initialized before y. The local data x is assigned to an empty array. If the configuration option -warningOnUninitializedData is set to true, a warning occurs.

  • If the chart has no data named y, x is initialized by calling the function y. If the file y.m is not on the search path, this error occurs:

    Undefined function or variable 'y'.

Stateflow does not search the MATLAB workspace to resolve initial values, so this error occurs even if there is a variable called y in the MATLAB workspace.

Capabilities and Limitations

Supported features:

  • Classic chart semantics with MATLAB as the action language. You can use the full functionality of MATLAB, including those functions that are restricted for code generation in Simulink. See Execute Stateflow Chart Objects Through Scripts and Models.


    In standalone Stateflow charts, the operating system command symbol ! is not supported. To execute operating system commands, use the function system.

  • Exclusive (OR) and Parallel (AND) state decomposition with hierarchy. See State Decomposition and State Hierarchy.

  • Flow charts, graphical functions, and MATLAB functions. See Reusable Components in Charts.

  • Chart local and constant data without restriction to type. See Execute and Unit Test Stateflow Chart Objects.

  • Input events. See Design Human-Machine Interface Logic by Using Stateflow Charts.

  • Temporal logic operators:

    • after, every, and temporalCount operate on the number of invocations (tick), absolute time (sec, msec, and usec), and input events.

    • count operates on the number of chart invocations (tick).

    • elapsed operates on absolute time (sec).

    For more information, see Design Human-Machine Interface Logic by Using Stateflow Charts.


    In standalone Stateflow charts, absolute time temporal logic is not supported in transitions with conditions.

  • Function getActiveStates to access the states that are active during execution of the chart. To store the active states as a cell array, enter:

    states = getActiveStates(chartObject)

  • Function matlab.internal.getcode.sfxfile that generates the code implementing the standalone chart as a MATLAB object. To store the code as a character string code, enter:

    code = matlab.internal.getcode.sfxfile('chart.sfx')
    To save the code in the file chart.m, enter:
    code = matlab.internal.getcode.sfxfile('chart.sfx',true)

Not supported (content specific to Simulink):

  • Sample time and continuous-time semantics.

  • C action language.

  • Simulink functions and Simulink subsystems as states.

  • Input, output, and parameter data.

  • Data store memory data.

  • Output and local events.

  • Input, output, and local messages.

Other limitations:

  • No Mealy or Moore semantics.

  • No State Transition Tables.

  • No Truth Table functions.

  • No state-parented local data or functions.

  • No transition actions (actions that execute after the source state for the transition is exited but before the destination state is entered).

  • No change detection operators such as hasChanged, hasChangedFrom, or hasChangedTo.

See Also

| |

Related Topics