This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Create and Access Charts Using the Stateflow API

Create a New Model and Chart

  1. Close all models.

  2. Type the function sfnew to create a new, untitled model with a new Stateflow® chart in it.

MATLAB® is the default action language of a chart you create with sfnew. To open a new C chart or to change the default action language, see Modify the Action Language for a Chart.

You have only one Simulink® model in memory. Do not open the chart. You can now access the API Model object that represents the model itself.

Access 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), etc.) 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 chart so that you can start filling it with Stateflow objects.

Access the Chart Object

In Access 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 chart:

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

    ch = 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, ch(1), ch(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.

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


    The preceding command calls the view method of the Chart object whose handle is ch. The specified chart appears. Other Stateflow API objects have view methods as well.

Create New Objects in the Chart

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

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

    sA = Stateflow.State(ch);

    This command is a Stateflow API constructor for a new state in which Stateflow.State is the object type for a state, ch 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 chart.

  2. Use the ch.view command to bring the chart 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(ch);
    sA1.Name = 'A1';
    sA1.Position = [80 120 90 60];
    sA2 = Stateflow.State(ch);
    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 appear as follows.

  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(ch);
    tA1A2.Source = sA1;
    tA1A2.Destination = sA2;
    tA1A2.SourceOClock = 3;
    tA1A2.DestinationOClock = 9;

    A transition now appears as shown.

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

    tA1A2.LabelPosition = [180 140 0 0];
    tA1A2.LabelString = 'E1';

    The chart now looks like this:

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

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

    pos = tA1A2.LabelPosition;
    pos(1) = pos(1)+5;
    tA1A2.LabelPosition = pos;
  8. 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:

    % Add a default transition to state A
    dtA = Stateflow.Transition(ch);
    dtA.Destination = sA;
    dtA.DestinationOClock = 0;
    xsource = sA.Position(1)+sA.Position(3)/2;
    ysource = sA.Position(2)-30;
    dtA.SourceEndPoint = [xsource ysource];
    dtA.MidPoint = [xsource ysource+15];
    % Add a default transition to state A1
    dtA1 = Stateflow.Transition(ch);
    dtA1.Destination = sA1;
    dtA1.DestinationOClock = 0;
    xsource = sA1.Position(1)+sA1.Position(3)/2;
    ysource = sA1.Position(2)-30;
    dtA1.SourceEndPoint = [xsource ysource];
    dtA1.MidPoint = [xsource ysource+15];

    Your complete chart looks like this:

  9. Save the model with the new chart to the current folder as myModel:

    sfsave(m.Name, 'myModel');

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

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

Was this topic helpful?