Teleport To Another State Without Using Transition Arrow?

211 views (last 30 days)
Is there a way to jump to another state by manipulating some variable rather than using a transition? I have a state that has many inflows and out flows from/to many states. My desired behavior is this: Say, you have a state with name S0 and the next state to jump to is saved in a variable NXT_STATE. I would like to jump to S1 by doing something like entry:Stateflow.Transition.Destination=NXT_STATE. Currently, I’m using super transitions since I have many interconnected state across different layers of charts.

Accepted Answer

Corey Lagunowich
Corey Lagunowich on 11 Feb 2026 at 20:43
Hi Amjad,
In general, you'll need some kind of incoming transition. However, there are many modeling patterns you could use, depending on your application, that might help to cut down on the number of transition paths you need to draw.
One possibility: Create a state that parents both S0 and S1. Draw an inner transition for the parent that connects to a junction, and have it guarded by [hasChanged(NXT_STATE)]. And then after the junction, draw transitions to both S0 and S1. Guard the path to S0 with [NXT_STATE == MyEnum.S0] and to S1 with [NXT_STATE == MyEnum.S1]. (this assumes you're creating a custom enumeration with all the state names. You could also do something simpler with just index values, like NXT_STATE == 0, NXT_STATE == 1, etc.)

More Answers (1)

Rajanya
Rajanya on 30 Sep 2024
Hi @Amjad,
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.
rt = sfroot()
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');
subchart = state3.Chart;
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!

Categories

Find more on Stateflow Programmatic Interface in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!