| Stateflow User's Guide | ![]() |
Creating a MATLAB Script of API Commands
In the Quick Start for the Stateflow API section, you created and saved a new model through a series of Stateflow API commands. You can include the same API commands in the following MATLAB script. This script allows you to quickly recreate the same model with the single command makeMyModel.
function makeMyModel % Get all previous models loaded rt = sfroot; prev_models = rt.find('-isa','Simulink.BlockDiagram'); % Create new model, and get current models sfnew; curr_models = rt.find('-isa','Simulink.BlockDiagram'); % New model is current models - previous models m = setdiff(curr_models, prev_models); % Get chart in new model chart = m.find('-isa', 'Stateflow.Chart'); % Create state A in chart sA = Stateflow.State(chart); sA.Name = 'A'; sA.Position = [45 45 300 150]; % Create state A1 inside of state A sA1 = Stateflow.State(chart); sA1.Name = 'A1'; sA1.Position = [80 80 90 80]; % Create state A2 inside of state A sA2 = Stateflow.State(chart); sA2.Name = 'A2'; sA2.Position = [220 80 90 80]; % Create a transition from A1 to A2 tA1A2 = Stateflow.Transition(chart); tA1A2.Source = sA1; tA1A2.Destination = sA2; tA1A2.SourceOClock = 3.; tA1A2.DestinationOClock = 9.; % Create state A11 inside of state A1 sA11 = Stateflow.State(chart); sA11.Name = 'A11'; sA11.Position = [110 110 35 35]; % Create a transition from A1 to A11 tA1A11 = Stateflow.Transition(chart); tA1A11.Source = sA1; tA1A11.Destination = sA11; tA1A11.SourceOClock = 1.; tA1A11.DestinationOClock = 1.; % Label transitions A1-A11 and A1-A2 to listen for events E1 and E2 tA1A2.LabelString = 'E1'; tA1A11.LabelString = 'E2'; % Create the Events E1 and E2 E1 = Stateflow.Event(chart); E1.Name = 'E1'; % Move label for transition A1-A1 to the right a bit pos = tA1A2.LabelPosition; pos(1) = pos(1)+15; tA1A2.LabelPosition = pos; % Create a default transition to state A dtA = Stateflow.Transition(chart); dtA.Destination = sA; dtA.DestinationOClock = 0; xsource = sA.Position(1)+sA.Position(3)/2-10; ysource = sA.Position(2)-20; dtA.SourceEndPoint = [xsource ysource]; % Create a default transition to state A1 dtA1 = Stateflow.Transition(chart); dtA1.Destination = sA1; dtA1.DestinationOClock = 0; xsource = sA1.Position(1)+sA1.Position(3)/2-10; ysource = sA1.Position(2)-20; dtA1.SourceEndPoint = [xsource ysource];
| Making Supertransitions | API Properties and Methods by Use | ![]() |
© 1994-2005 The MathWorks, Inc.