Main Content

Stateflow.Clipboard

Clipboard to copy and paste Stateflow objects

    Description

    Use the Stateflow.Clipboard object to copy and paste graphical and nongraphical objects within the same chart, between charts in the same Simulink® model, or between charts in different models.

    Creation

    There is only one Stateflow.Clipboard object, which is created automatically when you start Stateflow®. To access this object, call the sfclipboard function:

    clipboard = sfclipboard;

    Object Functions

    copyCopy array of objects to clipboard
    pasteToPaste objects in clipboard to specified container object

    Examples

    collapse all

    Group state A and copy its contents to chart ch. When you group a state, box, or graphical function, you can copy and paste all the objects contained in the grouped object, as well as all the relationships among these objects. This method is the simplest way of copying and pasting objects programmatically. If a state is not grouped, copying the state does not copy any of its contents.

    Stateflow chart with a hierarchy of states. The outer state is called A. It contains two inner states called A1 and A2.

    1. Find the Stateflow.State object named A in chart ch.

      sA = find(ch,'-isa','Stateflow.State','Name','A');
    2. Group state A and its contents by setting the IsGrouped property for sA to true. Save the previous setting of this property so you can revert to it later.

      prevGrouping = sA.IsGrouped;
      sA.IsGrouped = true;
    3. Change the name of the state to 'Copy_of_A'. Save the previous name so you can revert to it later.

      prevName = sA.Name;
      newName = ['Copy_of_' prevName];
      sA.Name = newName;
    4. Access the clipboard object.

      cb = sfclipboard;
    5. Copy the grouped state to the clipboard.

      copy(cb,sA);
    6. Restore the state properties to their original settings.

      sA.IsGrouped = prevGrouping;
      sA.Name = prevName;
    7. Paste a copy of the objects from the clipboard to the chart.

      pasteTo(cb,ch);
    8. Adjust the state properties of the new state.

      sNew = find(ch,'-isa','Stateflow.State','Name',newName);
      sNew.Position = sA.Position + [400 0 0 0];
      sNew.IsGrouped = prevGrouping;

      Chart showing result of pasting a copy of state A and its contents.

    Copy states A1 and A2, along with the transition between them, to a new state in chart ch. To preserve transition connections and containment relationships between objects, copy all the connected objects at once.

    Stateflow chart with a hierarchy of states. The outer state is called A. It contains two inner states called A1 and A2.

    1. Find the Stateflow.State object named A in chart ch.

      sA = find(ch,'-isa','Stateflow.State','Name','A');
    2. Add a new state called B. To enable pasting of other objects inside B, convert the new state to a subchart.

      sB = Stateflow.State(ch);
      sB.Name = 'B';
      sB.Position = sA.Position + [400 0 0 0];
      sB.IsSubchart = true;
    3. Create an array called objArray that contains the states and transitions in state A. Use the function setdiff to remove state A from the array of objects to copy.

      objArrayS = find(sA,'-isa','Stateflow.State');
      objArrayS = setdiff(objArrayS,sA);
      objArrayT = find(sA,'-isa','Stateflow.Transition');
      objArray = [objArrayS objArrayT];
    4. Access the clipboard object.

      cb = sfclipboard;
    5. Copy the objects in objArray and paste them in subchart B.

      copy(cb,objArray);
      pasteTo(cb,sB);
    6. Revert B to a state.

      sB.IsSubchart = false;
      sB.IsGrouped = false;
    7. Reposition the states and transitions in B.

      newStates = find(sB,'-isa','Stateflow.State');
      newStates = setdiff(newStates,sB);
      newTransitions = find(sB,'-isa','Stateflow.Transition');
      newOClocks = get(newTransitions,{'SourceOClock','DestinationOClock'});
      for i = 1:numel(newStates)
      newStates(i).Position = newStates(i).Position + [25 35 0 0];
      end
      set(newTransitions,{'SourceOClock','DestinationOClock'},newOClocks);

      Chart showing result of copying contents of state A and pasting them into state B.

    Introduced before R2006a