Access Objects in Your Stateflow Chart

You manipulate Stateflow® API objects through MATLAB® variables called handles. Once you have a handle to an API object, you can change the properties of the object, call methods on the object, and add new objects inside the object.

There are several ways to get handles for the API objects in a chart. For example, you can use the find and up methods to traverse the Stateflow hierarchy. You can also call the sfgco function to retrieve the most recently selected objects in a chart.

Find Handles to API Objects

With the find method, 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 method

  • A property name and value

For example, this command searches through the Root object rt and returns every State object with the name 'On':

onState = rt.find('-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 all Chart objects:

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

Find Objects at Different Levels of Containment

Once you have a handle to an API object, you can navigate through the Stateflow hierarchy and find the objects that it contains (its children) or the object that contains it (its parent). For example, in this chart, state A is the parent state of the child states A1 and A2.

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 method. By default, the find method finds objects at all depths of containment within an object. For instance, suppose that ch is a handle to the chart in the previous example. Calling the find method to find all the states in ch returns a vector of handles to three states:

states = ch.find('-isa','Stateflow.State');
states.get('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 State object at the first level of containment in ch, enter:

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

    'A'

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

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

  3×1 cell array

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

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

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

  2×1 cell array

    {'A1'}
    {'A2'}

Find a Parent Object

To find the parent of an API object, call the up method. The up method returns a handle to the parent container object of an object. For instance, suppose that sA1 is a handle to state A1 in the previous example. Calling the up method on sA1 returns a handle to the state A:

pA1 = sA1.up;
pA1.Name
ans =

    'A'

Similarly, calling the up method on pA1 returns a handle to the chart:

ppA1 = pA1.up;
ppA1.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 handle or a vector of handles 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 a handle to the corresponding 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 a vector of handles to the corresponding State objects.

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

  3×1 cell array

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

Note

The order of the State objects in the vector states depends on the order in which you select the states.

See Also

| | |

Related Topics