Copy and Paste Stateflow Objects

Access the Clipboard Object

The Clipboard object (only one exists) provides an interface to the clipboard used in copying Stateflow® objects. You cannot directly create or destroy the Clipboard object as you do other Stateflow API objects. However, you can attach a handle to it to use its properties and methods to copy Stateflow objects.

You create a handle to the Clipboard object by using the sfclipboard function as follows:

cb = sfclipboard;

Clipboard objects have two methods, copy and pasteTo, that together provide the functionality to copy objects from one object to another. The copy method copies the specified objects to the Clipboard object, and the pasteTo method pastes the contents of the clipboard to a new container.

copy Method Limitations

The copy method is subject to these limitations for all objects:

  • The objects you copy must be all graphical (states, boxes, functions, transitions, junctions) or all nongraphical (data, events, messages).

    You cannot copy a mixture of graphical and nongraphical objects to the clipboard in the same copy operation.

  • To maintain the transition connections and containment relationships between copied objects, you must copy the entire array of related objects.

    All related objects must be part of the array of objects copied to the clipboard. For example, if you try to copy two states connected by a transition to another container, you can only accomplish this by copying both the states and the transition at the same time. That is, you must do a single copy of a single array containing both the states and the transition that connects them.

    If you copy a grouped state to the clipboard, you copy all the objects contained in the state, as well as all the relations among the objects in the grouped state. See Copy by Grouping.

Copy Graphical Objects

The copy method is subject to these limitations for all graphical objects:

  • Copying graphical objects also copies the Data, Event, and Message objects that the graphical objects contain.

  • If all copied objects are graphical, they must all be visible in the same subviewer.

    In other words, all graphical objects copied in a single copy command must reside in the same chart or subchart.

Copy by Grouping

Copying a grouped state in a Stateflow chart copies not only the state but all of its contents. By grouping a state before you copy it, you can copy it and all of its contained objects at all levels of containment with the Stateflow API. This method is the simplest way of copying objects. Use it whenever possible.

You use the Boolean IsGrouped property for a state to group that state. If you set the IsGrouped property for a state to a value of true (=1), it is grouped. If you set IsGrouped to a value of false (=0), the state is not grouped.

This example procedure copies state A to the chart X through grouping. In this example, assume that you already have a handle to state A and chart X through the MATLAB® variables sA and chX, respectively:

  1. If the state to copy is not already grouped, group it along with its contents by setting the IsGrouped property for that state to true (=1).

    prevGrouping = sA.IsGrouped;
    if (prevGrouping == 0)
    	sA.IsGrouped = 1;
    end
  2. Get a handle to the Clipboard object.

    cb = sfclipboard;
  3. Copy the grouped state to the clipboard using the Clipboard object.

    cb.copy(sA);
  4. Paste the grouped object to its new container.

    cb.pasteTo(chX);
  5. Set the copied state and its source state to its previous IsGrouped property value.

    sA.IsGrouped=prevGrouping;
    sNew=chX.find('-isa','Stateflow.State','Name',sA.Name);
    sNew.IsGrouped=prevGrouping;

Copy Objects Individually

You can copy specific objects from one object to another. However, in order to preserve transition connections and containment relations between objects, you must copy all the connected objects at once. To accomplish this, use the general technique of appending objects from successive finds in the MATLAB workspace to a growing array of objects before copying the finished object array to the clipboard.

For the example, in the following chart, you can copy states A1 and A2, along with the transition between them, to a new state.

Suppose that ch is a handle to the chart.

  1. Find a handle to state A.

    sA = ch.find('-isa','Stateflow.State','Name','A');

  2. Add a new state B.

    sB = Stateflow.State(ch);
    sB.Name = 'B';

  3. Create an array sourceObjs containing handles to the states and transitions in state A.

    objArrayS = sA.find('-isa','Stateflow.State','-depth',1);
    objArrayT = sA.find('-isa','Stateflow.Transition','-depth',1);
    sourceObjs = [objArrayS ; objArrayT];

  4. Create a handle to the clipboard object.

    cb = sfclipboard;

  5. Copy the objects in sourceObjs and paste them in state B.

    cb.copy(sourceObjs);
    cb.pasteTo(sB);

You can also copy nongraphical data, event, and message objects individually. However, since there is no way for these objects to find their new owners, you must ensure that you copy each of these objects separately to its appropriate owner object.

Note

Copying objects individually is harder than copying grouped objects. See Copy by Grouping.

Related Topics