Main Content

Access Objects in Your Stateflow Chart

The Stateflow® API consists of objects that represent the graphical and nongraphical objects of a Stateflow chart. For example, the API objects Stateflow.State and Stateflow.Transition represent states and transitions in a Stateflow chart.

Find Objects in a Chart

With the find function, you specify search criteria for the API object that you want to locate. You can combine criteria such as:

  • The type of object

  • The name of a property or function

  • A property name and value

For example, this command searches the Simulink.Root object and returns every Stateflow.State object with the name 'On':

onState = find(sfroot,'-isa','Stateflow.State','Name','On')

If more than one object meets the search criteria, find returns an array of qualifying objects. For example, if more than one chart is open, this command returns an array of Stateflow.Chart objects:

chartArray = find(sfroot,'-isa','Stateflow.Chart')

Find Objects at Specific Levels of Containment

Once you access an API object, you can navigate through the Stateflow hierarchy and find its children (the objects that it contains) or its parent (the object that contains it). For example, in this chart, state A is the parent state of the child states A1 and A2. You can use the find and up functions to navigate from the chart to state A and its children, and back from the child states to the parent state and the chart. For more information on this example, see Create Charts by Using a MATLAB Script.

Stateflow chart with a hierarchy of states. The outer state is called A. It contains two inner states called A1 and A2.

Find Child Objects

To find the children of an API object, call the find function. By default, the find function finds objects at all depths of containment within an object. For instance, suppose that ch is the Stateflow.Chart object that corresponds to the chart in the previous example. Calling the find function to find all the states in ch returns an array with three Stateflow.State objects:

states = find(ch,'-isa','Stateflow.State');
get(states,'Name')
ans =

  3×1 cell array

    {'A'}
    {'A1'}
    {'A2'}

To limit the maximum containment depth of a search, use the '-depth' argument as part of your search criteria. For example, to find the only Stateflow.State object at the first level of containment in ch, enter:

sA = find(ch,'-isa','Stateflow.State','-depth',1);
sA.Name
ans =

    'A'

Similarly, to find all the states in A, you can call the find function on the Stateflow.State object sA. In this case, the search includes the zeroth level of containment, which is the searched object itself:

states = find(sA,'-isa','Stateflow.State');
get(states,'Name')
ans =

  3×1 cell array

    {'A'}
    {'A1'}
    {'A2'}

To exclude the state A from the search results, call the MATLAB® function setdiff:

childStates = setdiff(states,sA);
get(childStates,'Name')
ans =

  2×1 cell array

    {'A1'}
    {'A2'}

Find Parent Object

To find the parent of an API object, call the up function. For instance, suppose that sA1 is the Stateflow.State object that corresponds to state A1 in the previous example. Calling the up function on sA1 returns the Stateflow.State object that corresponds to the state A:

parent = up(sA1);
parent.Name
ans =

    'A'

Similarly, calling the up function on parent returns the Stateflow.Chart object that corresponds to the chart:

grandparent = up(parent);
grandparent.Name
ans =

    'Chart'

Retrieve Recently Selected Objects

You can retrieve the most recently selected objects in a chart by calling the sfgco function. This function returns a single object or an array of objects, depending on your selection.

For instance, consider the chart in the previous example. Suppose that you select the transition from state A1 to state A2. Calling sfgco returns the corresponding Stateflow.Transition object:

tr = sfgco;
str = ['Transition from ' tr.Source.Name ' to ' tr.Destination.Name]
str =

    'Transition from A1 to A2'

Similarly, if you simultaneously select the three states in the chart, calling sfgco returns an array of Stateflow.State objects.

states = sfgco;
get(states,'Name')
ans =

  3×1 cell array

    {'A'}
    {'A1'}
    {'A2'}

Note

When you use sfgco to access multiple objects, the order of the objects in the array depends on the order in which you select the objects.

See Also

Functions

Objects

Related Topics