Main Content

Create and Delete Stateflow Objects

The objects in the Stateflow® API 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. For more information, see Overview of the Stateflow API.

Create Stateflow Objects

Stateflow API objects are organized in the containment hierarchy described in Hierarchy of Stateflow API Objects. To create a Stateflow object as the child of a parent object, you begin by accessing the parent object. Then use the parent object as the input argument to a function that creates the child object. For example, to add a new Stateflow.State object in a Stateflow.Chart object, follow these steps:

  1. Access the parent object ch as described in Access Objects in Your Stateflow Chart.

  2. Call the Stateflow.State function using the parent object ch as an argument.

    st = Stateflow.State(ch);
  3. Display the new state in the Stateflow Editor by calling the view function. Use the Stateflow.State object as the argument to the function.


  4. Make changes to the state by modifying the properties of the Stateflow.State object. For example, you can set the name and position of the state by modifying the Name and Position properties. To set the Position property, specify the new position as a four-element vector in which the first two values are the (x,y) coordinates of the upper-left corner of the state and the last two values are the width and height of the state.

    st.Name = "A";
    st.Position = [30 30 90 60];

    You can also connect the new state to other states or junctions in your chart by creating a Stateflow.Transition object and setting its Source or Destination properties to st.

For an example of how to add states, transitions, and data objects to a chart, see Create Charts by Using the Stateflow API.

Graphical Object Containment

When you create a graphical object such as a state, function, box, junction, or annotation, it appears in the upper-left corner of its parent object. You can move the graphical object to a different location by modifying its Position property, as explained in the previous example.

When you create a transition, it appears in the upper-left corner of the chart or subchart where you can view the parent object. You can move the transition to a different location by setting its source and destination or by modifying its SourceEndPoint, MidPoint, and DestinationEndPoint properties.

A graphical object must be located inside the boundary of its parent. Repositioning a graphical object can change its parent or result in an undefined parent error. You can check for this condition by examining the value of the BadIntersection property of an object. This property is true if the edges of the graphical object overlap with another graphical object. Set the position and size of objects so that they are separate from other objects.

You cannot move an object in a subcharted state, box, or graphical function to a different level of the chart hierarchy by changing its position. Instead, copy and paste the object from one parent object to another. Then delete the original object. For more information, see Copy and Paste by Grouping and Copy and Paste Array of Objects.

Nongraphical Object Containment

When you create nongraphical objects such as data, events, or messages, they appear in the Model Explorer and in the Symbols pane at the hierarchical level of their parent object. You can also see the location of the parent object by inspecting the Path property of an object.

You cannot change the parent of a nongraphical object programmatically. Instead, use the Model Explorer. For more information, see Use the Model Explorer with Stateflow Objects.

Delete Stateflow Objects

You can delete most objects in a Stateflow chart by calling the function delete. For example, to delete a Stateflow.State object st, enter:


After you delete the state, the variable st still exists in the MATLAB® workspace, but it is no longer associated with the state.

Deleting a graphical object also deletes the nongraphical children of the deleted object. The graphical children of the deleted object become children of the parent of the deleted object. (since R2023a)


You cannot use the delete function to delete objects of these types:

  • Simulink.Root

  • Stateflow.Machine

  • Stateflow.Chart

  • Stateflow.EMChart

  • Stateflow.StateTransitionTableChart

  • Stateflow.TruthTableChart

  • Stateflow.Clipboard

  • Stateflow.Editor

See Also



Related Topics