Close all models.
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
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.
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:
Use this command to obtain a handle to the Root object:
rt = sfroot;
Use the handle to the Root object,
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 (
etc.) You can identify a specific Model object using the properties
of each model, particularly the
which is the name of the model. For example, you can use the
to find a Model object named myModel with
m = rt.find('-isa', 'Simulink.BlockDiagram', '-and', ... 'Name', 'myModel');
However, since you now have only one model loaded, the object
m in the command for step 2 returns the
Model object for the model that you just created. You are now ready
m to access the empty chart so that you
can start filling it with Stateflow objects.
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
m. Perform these steps to access the new
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
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
returns an array of charts that you could access through indexing
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,
ch = find(m, '-isa','Stateflow.Chart');
The preceding command calls the
view method of the Chart object whose
ch. The specified chart appears. Other Stateflow API
view methods as well.
In the previous section, you created a handle to the new Chart
ch. Continue by creating new objects for
your chart using these steps:
sA = Stateflow.State(ch);
This command is a Stateflow API constructor for a new state
Stateflow.State is the object type for
ch is a workspace variable containing
a handle to the parent chart of the new state, and
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.
to bring the chart to the foreground for viewing.
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];
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
handles to the new states, which now appear as follows.
tA1A2 = Stateflow.Transition(ch); tA1A2.Source = sA1; tA1A2.Destination = sA2; tA1A2.SourceOClock = 3; tA1A2.DestinationOClock = 9;
A transition now appears as shown.
Add the label
the transition from state
A1 to state
tA1A2.LabelPosition = [180 140 0 0]; tA1A2.LabelString = 'E1';
The chart now looks like this:
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
you can use to move the labels to better locations.
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;
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:
Save the model
with the new chart to the current folder as
This command uses the
Name property of the
m for saving the model under a new
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.