Accessing Existing Stateflow Objects

www.kxcad.net Home > CAE Software Index > MATLAB Index >


Your Ad Here

About Stateflow Object Handles

Creating Stateflow objects through the Stateflow API gives you an immediate handle to the newly created objects (see Creating Stateflow Objects). You can also connect to Stateflow objects that already exist for which you have no current API handle.

Finding Objects

There are several object methods that you use to traverse the Stateflow hierarchy to locate existing objects. Chief among these is the versatile find method.

With the find method, you specify what to search for by specifying combinations of the following types of information:

The following example searches through Model object m to return every State object with the name 'On'.

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

If a find command finds more than one object that meets its specifications, it returns an array of qualifying objects. The following example returns an array of all charts in your model:

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

Use array indexing to access individual properties and methods for a chart. For example, if the preceding command returns three Stateflow charts, the following command returns the Name property of the second chart found:

name2 = chartArray(2).Name

By default, the find command finds objects at all depths of containment within an object. This includes the zeroth level of containment, which is the searched object itself. For example, if state A, which is represented by State object sA, contains two states, A1 and A2, and you specify a find command that finds all the states in A as follows,

states= sA.find( '-isa','Stateflow.State')

The preceding command finds three states: A, A1, and A2.

Finding Objects at Different Levels of Containment

Once you find a particular object in a Stateflow diagram by its name or another property, you might want to find the objects that it contains (children), or the object that contains it (parent). To find child objects, use the find method. To find a parent object, use the method up.

Finding Child Objects

The find method finds objects at the depth of containment within an object that you specify. If you want to limit the containment search depth with the find command, use the depth switch. For example, to find all the objects in State object sA at the first level of containment, use the following command:

objArray = sA.find('-depth', 1)

Don't forget, however, that the find command always includes the zeroth level of containment, which is the object itself. So, the preceding command also includes state A in the list of objects found. However, you can exclude state A from the vector of objects in objArray with the MATLAB function setdiff as follows:

objArray = setdiff(objArray, sA)

The following command returns a collection of all junctions at the first level of containment inside the state A that is represented by State object sA:

juncArray = sA.find('-isa','Stateflow.Junction','-depth',1)

The following command returns an array of all transitions inside state A at all levels of containment:

transArray = sA.find('-isa','Stateflow.Transition')

Finding a Parent Object

The up method finds the parent container object of any given object. In the example Stateflow diagram in Create New Objects in the Chart, state A contains states A1 and A2. Also, state A1 contains state A11. In the example, sA11 is a handle to the state A11. This means that

>> pA11 = sA11.up;
>> pA11.Name

ans =

A1

returns a handle pA11 to the state A1, the parent of state A11, and

>> ppA11 = pA11.up;
>> ppA11.Name

ans =

A

returns a handle ppA11 to the state A, the parent of state A1.

Retrieving Recently Selected Objects

You can retrieve the most recently selected objects in a Stateflow diagram by using the sfgco function. This function returns object handles or a vector of handles depending on the following conditions:

IfThen sfgco returns
There are no open diagramsAn empty matrix
There is no selection listHandle of the diagram most recently clicked
You select one object in a diagramHandle to the selected object
You select multiple objects in a diagramVector of handles for the selected objects
You select objects in multiple diagramsHandles of the most recently selected objects in the most recently selected diagram

For example, suppose you run the sf_boiler demo and open the Stateflow chart called Bang Bang Controller. If you select the Off state in the chart, sfgco returns

ans =
 
               Path: [1x37 char]
                 Id: 31
            Machine: [1x1 Stateflow.Machine]
               Name: 'Off'
        Description: ''
        LabelString: [1x56 char]
           FontSize: 12
          ArrowSize: 8
          TestPoint: 0
              Chart: [1x1 Stateflow.Chart]
    BadIntersection: 0
          Subviewer: [1x1 Stateflow.Chart]
           Document: ''
                Tag: []
    RequirementInfo: ''
     ExecutionOrder: 0
      HasOutputData: 0
           Position: [31.7440 40.9730 214.1620 59.5660]
      Decomposition: 'EXCLUSIVE_OR'
               Type: 'OR'
         IsSubchart: 0
          IsGrouped: 0
              Debug: [1x1 Stateflow.StateDebug]

If you then hold down the Shift key to select the three transitions in state Off, sfgco returns

ans =
 
	Stateflow.Transition: 3-by-1

Getting and Setting the Properties of Objects

Once you obtain a particular object, you can access its properties directly or through the get method. For example, you obtain the description for a State object s with one of the following commands:

You change the properties of an object directly or through the set method. For example, you change the description of the State object s with one of the following commands:

  


© 1984-2007 The MathWorks, Inc. Terms of Use Patents Trademarks Acknowledgments

Your Ad Here