Create Stateflow Charts for Execution as MATLAB Objects - MATLAB & Simulink (original) (raw)

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:

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.

In the Stateflow Editor, create a standalone chart by combining states, transitions, data, and other elements. For more information, see Create Stateflow Charts.

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

To close the standalone chart from the MATLAB Command Window, use the sfclose function:

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, suppose that you defined a standalone chart with data objects called data1 and data2. Then this command creates the chart object chartObject, initializesdata1 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 thestep function (with data values, if necessary):

step(chartObject,data1=value1,data2=value2)

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

event_name(chartObject,data1=value1,data2=value2)

In either case, the values are assigned to local data before the chart executes.

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.

function_name(chartObject,u1,u2)

Note

If you use nargin in a graphical or MATLAB function in your chart, nargin counts the chart object as one of the input arguments. The value of nargin is the same whether you call the function from the chart or from the MATLAB Command Window.

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:

Stop Chart Execution

To stop executing a chart, destroy the chart object by calling the delete function:

After the chart object is deleted, any handles to the chart object remain in the workspace, but become invalid. To remove the invalid handle from the workspace, use the command clear:

If you clear a valid chart object handle and there are other handles to the same chart object, the chart object is not destroyed. For example, when you are executing a chart, the Stateflow Editor contains internal handles to the chart object. Clearing the chart object handle from the workspace does not destroy the chart object or remove the chart animation highlighting in the editor. To reset the animation highlighting, right-click the chart canvas and select .

Share Standalone Charts

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

If your collaborators have the same or a later version of MATLAB than you have, they can execute your standalone charts as MATLAB objects without opening the Stateflow Editor. Chart animation and debugging are not supported. Run-time error messages do not link to the state or transition in the chart where the error occurs.

Note

To run standalone charts that you saved in R2019a or R2019b, your collaborators must have the same version of MATLAB.

If your collaborators have an earlier version of MATLAB, export a standalone chart to a format that they can use. You can only export to R2019a and later releases. To complete the export process, you need access to the versions of Stateflow from which and to which you are exporting.

  1. Using the later version of Stateflow, open the standalone chart.
  2. On the State Chart tab, select > .
  3. In the Export to Previous Version dialog box, specify a file name for the exported chart.
  4. From the Save as type list, select the earlier version to which to export the chart.
  5. Click Save.
  6. Using the earlier version of Stateflow, open and resave the exported chart.

To export a chart from the MATLAB Command Window, call the Stateflow function exportToVersion. For more information, see Export Chart to an Earlier Version of MATLAB.

Note

Attempting to execute an exported chart before resaving it will result in an error.

Properties and Functions of Stateflow Chart Objects

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

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

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 Option Description Example
-animationDelay Specify the delay that the chart animation uses to highlight each transition segment. The default value is0.01 seconds. To produce a chart with no animation delays, set to zero. Create a chart object that has slow animation by specifying one-second delays.chartObject = chart('-animationDelay',1)
-enableAnimation Enable chart animation and debugging instrumentation. The default value is true. Create a chart object that has animation and debugging instrumentation disabled.chartObject = chart('-enableAnimation',false)
-eventQueueSize Specify the size of the queue used for events and temporal logic operations. The default value is20. To disable the queuing of events, set to zero. For more information, see Events in Standalone Charts. Create a chart object that ignores all events without warning if they occur when the chart is processing another operation.chartObject = chart('-eventQueueSize',0)
-executeInitStep Enable 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)
-warningOnUninitializedData Enable the warning about empty chart data after initializing the chart object. The default value istrue. 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 pane 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:

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

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

Limitations

Content specific to Simulink:

Other limitations:

See Also

disp | edit | exportAsClass | exportToVersion | help | sfclose

Topics