Quick Start for the Stateflow® API

Creating a New Model and Chart

Create a new model by following these steps:

  1. Close down all Simulink® models.

  2. Use the function sfnew to create a new chart.

    The sfnew function creates a new untitled Simulink model with a new Stateflow® chart in it. Do not open the Stateflow chart.

You now have only one Simulink model in memory. You are now ready to access the API Model object that represents the model itself.

Accessing the Model Object

In the Stateflow API, each model you create or load into memory is represented by an object of type Model. Before accessing the Stateflow chart you created in the previous section, you must first connect to its Model object. However, in the Stateflow API, all Model objects are contained by the Stateflow API Root object, so you must use the Root object returned by the function sfroot to access a Model object:

  1. Use this command to obtain a handle to the Root object:

    rt = sfroot
    
  2. Use the handle to the Root object, rt, to find the Model object representing your new untitled Simulink model and assign it a handle m in this command:

    m = rt.find('-isa','Simulink.BlockDiagram')
    

If, instead of one model, there are several models open, this command returns an array of different Model objects that you can access through indexing (m(1),m(2),...). You can identify a specific Model object using the properties of each model, particularly the Name property, which is the name of the model. For example, you can use the Name property to find a Model object named myModel with this command:

m = rt.find('-isa', 'Simulink.BlockDiagram', '-and', 
'Name','myModel')

However, since you now have only one model loaded, the object handle m in the command for step 2 returns the Model object for the model that you just created. You are now ready to use m to access the empty Stateflow chart so that you can start filling it with Stateflow objects.

Accessing the Chart Object

In Accessing the Model Object, you accessed the Model object containing your new chart to return a handle to the Model object for your new model, m. Perform these steps to access the new Stateflow chart:

  1. Access the new Chart object and assign it to the workspace variable chart as follows:

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

    In the preceding command, the find method of the Model object m returns an array of all charts belonging to that model. Because you created only one chart, the result of this command is the chart you created. If you created several charts, the find method returns an array of charts that you could access through indexing (for example, chart(1), chart(2), and so on).

    You can also use standard function notation instead of dot notation for the preceding command. In this case, the first argument is the Model object handle, m.

    chart = find(m, '-isa','Stateflow.Chart')
    
  2. Open the Stateflow chart with this API command:

    chart.view
    

    The preceding command calls the view method of the Chart object whose handle is chart. The specified chart appears in the Stateflow Editor. You should now have an empty Stateflow chart in front of you. Other Stateflow API objects have view methods as well.

Creating New Objects in the Chart

In the previous section, you created a handle to the new Chart object, chart. Continue by creating new objects for your chart using these steps:

  1. Create a new state in the Chart object chart with this command:

    sA = Stateflow.State(chart)
    

    This command is a Stateflow API constructor for a new state in which Stateflow.State is the object type for a state, chart is a workspace variable containing a handle to the parent chart of the new state, and sA is a workspace variable to receive the returned handle to the new state.

    An empty state now appears in the upper left-hand corner of the Stateflow Editor.

  2. Use the chart.view command to bring the Stateflow Editor to the foreground for viewing.

  3. Assign a name and position to the new state by assigning values to the properties of the new State object as follows:

    sA.Name = 'A'
    sA.Position = [50 50 310 200]
    
  4. Create new states A1 and A2 inside state A and assign them properties with these commands:

    sA1 = Stateflow.State(chart)
    sA1.Name = 'A1'
    sA1.Position = [80 120 90 60]
    sA2 = Stateflow.State(chart)
    sA2.Name = 'A2'
    sA2.Position = [240 120 90 60]
    

    These commands create and use the workspace variables sA, sA1, and sA2 as handles to the new states, which now have the following appearance:

  5. Create a transition from the 3 o'clock position (right side) of state A1 to the 9 o'clock position (left side) of state A2 with these commands:

    tA1A2 = Stateflow.Transition(chart)
    tA1A2.Source = sA1
    tA1A2.Destination = sA2
    tA1A2.SourceOClock = 3.
    tA1A2.DestinationOClock = 9.
    

    A transition now appears as shown:

  6. Draw, name, and position a new state A11 inside A1 with these commands:

    sA11 = Stateflow.State(chart)
    sA11.Name = 'A11'
    sA11.Position = [90 130 35 35]
    
  7. Draw an inner transition from the 1 o'clock position of state A1 to the 1 o'clock position of state A11 with these commands:

    tA1A11 = Stateflow.Transition(chart)
    tA1A11.Source = sA1
    tA1A11.Destination = sA11
    tA1A11.SourceOClock = 1.
    tA1A11.DestinationOClock = 1.
    

    Your Stateflow chart now has the following appearance:

  8. Add the label E1 to the transition from state A1 to state A2 with this command:

    tA1A2.LabelString = 'E1'
    
  9. Add the label E2 to the transition from state A1 to state A11 with this command:

    tA1A11.LabelString = 'E2'
    

    The Stateflow chart now has the following appearance:

    Both the state and transition labels in our example are simple one-line labels. To enter more complex multiline labels, see Entering Multiline Labels. Labels for transitions also have a LabelPosition property you can use to move the labels to better locations.

  10. Use these commands to move the label for the transition from A1 to A2 to the right by 15 pixels:

    pos = tA1A2.LabelPosition
    pos(1) = pos(1)+15
    tA1A2.LabelPosition = pos
    
  11. Use these commands to finish your new chart by adding default transitions to states A and A1 with source points 20 pixels above and 10 pixels to the left of the top midpoint of each state:

    dtA = Stateflow.Transition(chart)
    dtA.Destination = sA
    dtA.DestinationOClock = 0
    xsource = sA.Position(1)+sA.Position(3)/2-10
    ysource = sA.Position(2)-20
    dtA.SourceEndPoint = [xsource ysource]
    dtA1 = Stateflow.Transition(chart)
    dtA1.Destination = sA1
    dtA1.DestinationOClock = 0
    xsource = sA1.Position(1)+sA1.Position(3)/2-10
    ysource = sA1.Position(2)-20
    dtA1.SourceEndPoint = [xsource ysource]
    

    You now have this complete Stateflow chart:

  12. Save the Simulink model with its new Stateflow chart to the working directory as myModel.mdl with this command:

    sfsave(m.Name, 'myModel')
    

    Notice that the preceding command uses the Name property of the Model object m for saving the model under a new name.

You are now finished with Quick Start for the Stateflow® API. You can continue with Accessing the Properties and Methods of Objects, or you can go to Creating a MATLAB® Script of API Commands to see how to create a script of the API commands you used in this Quick Start section.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS