Teleport To Another State Without Using Transition Arrow?
1 view (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Rajanya
on 30 Sep 2024
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:
supertranstions - https://in.mathworks.com/help/releases/R2022a/stateflow/ug/moving-between-different-levels-of-hierarchy-with-supertransitions.html
Hope this helps!
0 Comments
See Also
Categories
Find more on Complex Logic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!