|On this page…|
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.
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:
rt = sfroot;
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.
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:
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');
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.
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:
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.
sA.Name = 'A'; sA.Position = [50 50 310 200];
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.
tA1A2 = Stateflow.Transition(ch); tA1A2.Source = sA1; tA1A2.Destination = sA2; tA1A2.SourceOClock = 3; tA1A2.DestinationOClock = 9;
A transition now appears as shown.
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.
pos = tA1A2.LabelPosition; pos(1) = pos(1)+5; tA1A2.LabelPosition = pos;
% 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:
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.