Main Content

CreateStateflowCharts for Execution asMATLABObjects

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.sfxand 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, seeShare Standalone Charts.

Construct a Standalone Chart

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

editchart.sfx
If the filechart.sfxdoes not exist, the Stateflow Editor opens an empty chart with the namechart. Otherwise, the editor opens the chart defined by thesfxfile.

In the Stateflow Editor, create a standalone chart by combining states, transitions, data, and other elements. For more information, seeConstruct and Run a Stateflow Chart.

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

helpchart.sfx

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

sfclosechart

Create aStateflowChart Object

To execute a standalone chart in MATLAB, first create a Stateflow chart object. Use the name of thesfxfile 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 calleddata1anddata2. Then this command creates the chart objectchartObject, initializesdata1anddata2, and executes its default transition:

chartObject =图(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 thedispfunction:

disp(chartObject)

Execute a Standalone Chart

After you define a Stateflow chart object, you can execute the standalone chart by calling thestepfunction (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 usenarginin a graphical or MATLAB function in your chart,nargincounts the chart object as one of the input arguments. The value ofnarginis 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, seeExecute 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 thedeletefunction:

delete(chartObject)

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 commandclear:

clearchartObject

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 selectRemove Highlighting.

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 theState Charttab, selectSave>Previous Version.

  3. In the Export to Previous Version dialog box, specify a file name for the exported chart.

  4. From theSave as typelist, select the earlier version to which to export the chart.

  5. ClickSave.

  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 functionexportToVersion. For more information, seeExport 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 ofStateflowChart 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.

  • Astepfunction 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 公共属性
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

当你创建一个图表对象,您可以指定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.01seconds. 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 =图('-animationDelay',1)

-enableAnimation Enable chart animation and debugging instrumentation. The default value istrue.

Create a chart object that has animation and debugging instrumentation disabled.

chartObject =图('-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, seeEvents in Standalone Charts.

Create a chart object that ignores all events without warning if they occur when the chart is processing another operation.

chartObject =图('-eventQueueSize',0)

-executeInitStep Enable the initial execution of default transitions. The default value istrue.

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

chartObject =图('-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 =图('-warningOnUninitializedData',false)

Initialization of Chart Data

In the Stateflow Editor, you can use theSymbolspane 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 dataxby using the expressiony. Then:

  • If the chart has a constant calledy,yis initialized beforex. The local dataxis assigned the same initial value asy.

  • If the chart has a local data calledy,xis initialized beforey. The local dataxis assigned to an empty array. If the configuration option-warningOnUninitializedDatais set totrue, a warning occurs.

  • If the chart has no data namedy,xis initialized by calling the functiony. If the filey.mis not on the search path, this error occurs:

    Undefined function or variable 'y'.

Stateflow不搜索MATLAB工作区resolve initial values, so this error occurs even if there is a variable calledyin 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. SeeExecute Stateflow Chart Objects Through Scripts and Models.

    Note

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

  • Exclusive (OR) and Parallel (AND) state decomposition with hierarchy. SeeDefine Exclusive and Parallel Modes by Using State DecompositionandUse State Hierarchy to Design Multilevel State Complexity.

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

  • Conversion of MATLAB code to graphical functions by using the Pattern Wizard. SeeConvert MATLAB Code into Stateflow Flow Charts.

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

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

  • OperatorshasChanged,hasChangedFrom, andhasChangedTothat detect changes in the values of local data.

    Note

    Standalone Stateflow charts do not support change detection on an element of a matrix or a field in a structure.

  • Temporal logic operators:

    • after,at, andeveryoperate on the number of input events, chart invocations (tick), and absolute time (sec). Use these operators in stateonactions and as transition triggers.

    • countoperates on the number of chart invocations (tick).

    • temporalCountoperates on absolute time (sec,msec, andusec).

    • elapsedoperates on absolute time (sec).

    Standalone charts define absolute-time temporal logic in terms of wall-clock time, which is limited to 1 millisecond precision.

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

    states = getActiveStates(chartObject)

  • Stateflow functionexportAsClassthat exports the standalone chart as the equivalent MATLAB class. Use this function to debug run-time errors that are otherwise difficult to diagnose. For example, suppose that you encounter an error while executing a Stateflow chart that controls a MATLAB application. If you export the chart as a MATLAB class file, you can replace the chart with the class in your application and diagnose the error by using the MATLAB debugger. To export the chartchart.sfxas a class filechart.m, enter:

    Stateflow.exportAsClass("chart.sfx")
    When you execute the MATLAB class, the Stateflow Editor does not animate the original chart.

Limitations

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).

See Also

|||||

Related Topics