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
With standalone charts, you can create MATLAB applications such as:
MATLAB App Designer user interfaces that use mode logic to manage the behavior of widgets. See Design Human-Machine Interface Logic by Using Stateflow Charts.
Communication protocols and data stream processing applications that use sequential logic. See Model a Communications Protocol by Using Chart Objects.
Data Acquisition Toolbox™ or Instrument Control Toolbox™ applications that use timer-based logic to monitor and control external tasks. See Implement a Financial Strategy by Using Stateflow.
These applications can be shared and executed without requiring a Stateflow license. For more information, see Share Standalone Charts.
To construct a standalone Stateflow chart, open the Stateflow editor by using the
edit function. For example, at the MATLAB Command Window,
chart.sfxdoes not exist, the Stateflow editor opens an empty chart with the name
chart. Otherwise, the editor opens the chart defined by the
After you save a standalone chart, the
help function displays information
about executing it in MATLAB:
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
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
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:
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.
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.
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 Elements||Chart Object Elements|
|Local and constant data||Public properties|
|Input events||Functions that execute the chart|
|Graphical and MATLAB functions||Functions that you can call from the MATLAB Command Window|
When you create a chart object, you can specify chart behavior by including these configuration options as name-value pairs.
|Specify the delay that the chart animation uses to
highlight each transition segment. The default value is
Create a chart object that has very slow animation by specifying one second delays:
chartObject = chart('-animationDelay',1)
|Enable chart animation. The default value is
Create a chart object that has animation disabled:
chartObject = chart('-enableAnimation',false)
|Enable the initial execution of default transitions. The
default value is |
Create a chart object but do not execute the default transition:
chartObject = chart('-executeInitStep',false)
|Enable the warning about empty chart data after
initializing the chart object. The default value is
Eliminate the warning when creating a chart object:
chartObject = chart('-warningOnUninitializedData',false)
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
x by using the expression
If the chart has a constant called
y is initialized before
The local data
x is assigned the same initial
If the chart has a local data called
x is initialized before
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
x is initialized by calling the function
y. If the file
y.m is not
on the search path, this error
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.
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
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:
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.
getActiveStates to access the states that
are active during execution of the chart. To store the active states as
a cell array,
states = getActiveStates(chartObject)
generates the code implementing the standalone chart as a MATLAB object. To store the code as a character string
code = matlab.internal.getcode.sfxfile('chart.sfx')
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.
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).