Teleport To Another State Without Using Transition Arrow?

2 次查看(过去 30 天)
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.

回答(1 个)

Rajanya
Rajanya 2024-9-30
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!

类别

Help CenterFile Exchange 中查找有关 Complex Logic 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by