Stateflow    

Create 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 the following steps:

  1. Create a new state in the Chart object chart with the following command:
  1. 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 diagram editor.

  1. Use the chart.view command to bring the chart diagram editor to the foreground for viewing.
  2. Assign a name and position to the new state by assigning values to the new State object's properties as follows:
  3. Create new states A1 and A2 inside state A and assign them properties with the following commands:
  1. These commands create and use the workspace variables sA, sA1, and sA2 as handles to the new states, which now have the following appearance:

  1. 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 the following commands:
  1. A transition now appears as shown:

  1. Draw, name, and position a new state A11 inside A1 with the following commands:
  2. Draw an inner transition from the 1 o'clock position of state A1 to the 1 o'clock position of state A11 with the following commands:
  1. Your Stateflow diagram now has the following appearance:

  1. Add the label E1 to the transition from state A1 to state A2 with the following command:
  2. Add the label E2 to the transition from state A1 to state A11 with the following command:
  1. The Stateflow diagram 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 that you can use to move the labels to better locations.

  1. Use the following commands to move the label for the transition from A1 to A2 to the right by 15 pixels:
  2. Use the following commands to finish your new chart diagram 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:
  1. You now have the following finished Stateflow diagram:

  1. Save the Simulink model with its new Stateflow chart to the working directory as myModel.mdl with the following command:
  1. Notice that the preceding command uses the Id property of the Machine object m for saving the model under a new name.

You are now finished with the Quick Start for the Stateflow API section of this chapter. You can continue with the next section (Accessing the Properties and Methods of Objects), or you can go to the section 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.


  Access the Chart Object Accessing the Properties and Methods of Objects