I understand that you are trying to control the state-transitioning in Stateflow programmatically for both in-chart transitions and supertransitions.
You can have external control over the state transitions using ‘Stateflow.Transition’ properties, but a good point to be noted would be that ‘Stateflow.Transition’ only accepts a Stateflow ’Box’, ‘Chart’, ‘Function’ or ‘State’ as the input argument to it’s constructor call.
Since here, you want to create transitions in the chart, you need a ‘Stateflow.Chart’ type object with which you can create the ‘Transition’ object. You can get the handle to the root of the Stateflow hierarchy using the ‘sfroot’ function and then the handles to the respective states and subcharts using ‘find’ method.
To demonstrate the working, I’ve created a sample Stateflow chart with 2 states and a sub-chart, ‘State1’, ‘State2’ and ‘Subchart1’.
‘Subchart1’ further contains two states ‘State4’ and ‘State5’.
chart = rt.find('-isa', 'Stateflow.Chart', 'Name', 'Chart');
state1 = chart.find('-isa', 'Stateflow.State', 'Name', 'State1');
state2 = chart.find('-isa', 'Stateflow.State', 'Name', 'State2');
state3 = chart.find('-isa', 'Stateflow.State', 'Name', 'Subchart1');
state4 = subchart.find('-isa', 'Stateflow.State', 'Name', 'State4');
With all the appropriate handles at disposal, transitions on the same level of chart hierarchy can be set programmatically by exploiting the ‘Source’ and ‘Destination’ properties of ‘Stateflow.Transition’, which accepts ‘State’ type objects as parameters.
transition = Stateflow.Transition(chart);
transition.Source = state1;
transition.Destination = state2;
This creates a transition line between ‘state1’ and ‘state2’. If you have multiple states which are in the same level as the source, you can also conditionally store the destination state in a variable which stores ‘State’ type objects.
For creating supertransitions which go across different levels of chart hierarchy, one way is to use ‘Ports’ of Stateflow. Normal usage of ‘Stateflow.Transition’ properties will throw error.
For the correct usage of ports, ensure that the entry port stays in an atomic model with every entry port correctly corresponding to it’s exit port.
port1 = Stateflow.Port(state3,'EntryJunction');
transition = Stateflow.Transition(state3);
transition.Source = port1;
transition.Destination = state4;
port2 = Stateflow.Port(chart,'ExitJunction');
transition5 = Stateflow.Transition(chart);
transition5.Source = state2;
transition5.Destination = port2;
The final chart, after adding all proper transitions, looks like this:
The following documentation links can prove helpful:
Hope this helps!