create stateflow charts for execution as matlab objects -凯发k8网页登录

main content

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

in the stateflow editor, create a standalone chart by combining states, transitions, data, and other elements. for more information, see .

after you save the standalone chart, the function displays information about executing it in matlab:

help chart.sfx

to close the standalone chart from the matlab command window, use the function:

sfclose chart

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, 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 function:

disp(chartobject)

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

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

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

clear chartobject

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 remove 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 the state chart tab, select save > previous version.

  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 . for more information, see .

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:

  • 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 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)
-enableanimationenable 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)
-eventqueuesizespecify the size of the queue used for events and temporal logic operations. the default value is 20. to disable the queuing of events, set to zero. for more information, see .

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

chartobject = chart('-eventqueuesize',0)
-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 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:

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

    note

    in standalone stateflow charts, the operating system command symbol ! is not supported. to execute operating system commands, use the function .

  • exclusive (or) and parallel (and) state decomposition with hierarchy. see and .

  • flow charts, graphical functions, and matlab functions. see .

  • conversion of matlab code to graphical functions by using the pattern wizard. see .

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

  • operators , , and that 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:

    • , , and operate on the number of input events, chart invocations (tick), and absolute time (sec). use these operators in state on actions and as transition triggers.

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

    • operates on absolute time (sec, msec, and usec).

    • operates on absolute time (sec).

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

  • 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)
  • stateflow function that 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 chart chart.sfx as a class file chart.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

网站地图