How to find all default transitions in stateflow programmatically?
4 views (last 30 days)
Show older comments
I want to be able to find all default transitions in a stateflow (in Charts,subcharts,etc). I am using find('-isa','Stateflow.Transition','-and','Source',[]) But this is not accurate and i am getting transitions that are just not connected to a source. Is there any other way ?
1 Comment
Muthukumar Ganesan
on 22 Jul 2022
Hi,
I'm wondering, is there any way to have a transition without a source except default transition. I think the above command would be sufficient, may be as an additional check you can include "Source0block=0".
Thanks.
Answers (1)
Purvaja
on 25 Aug 2025
Hello @TPar
I see that you wish to segregate default transitions from dangling transitions. You can achieve this by collecting all the transitions in a chart and classifying them based on their source and destination objects. By applying simple conditions, you can then filter or exclude whichever category you don’t need.
I have pasted below a copy of the MATLAB code I used, along with the output it generated and the corresponding Stateflow diagram. Feel free to adjust the code to suit your exact requirement (e.g., ignoring dangling transitions, or printing only defaults).
Stateflow dummy model:

Code:
% % Load the model that consists of the chart you wish to find from.
for c = charts'
% Get all transitions inside this chart
transitions = c.find('-isa','Stateflow.Transition');
for t = transitions'
src = t.Source;
dst = t.Destination;
% --- Format source ---
if isempty(src)
srcStr = '[NONE]';
elseif isa(src,'Stateflow.State')
srcStr = ['State:' src.Name];
elseif isa(src,'Stateflow.Junction')
srcStr = sprintf('Junction(ID=%d)', src.Id);
else
srcStr = class(src);
end
% --- Format destination ---
if isempty(dst)
dstStr = '[NONE]';
elseif isa(dst,'Stateflow.State')
dstStr = ['State:' dst.Name];
elseif isa(dst,'Stateflow.Junction')
dstStr = sprintf('Junction(ID=%d)', dst.Id);
else
dstStr = class(dst);
end
% --- Print classification ---
if isempty(src) && ~isempty(dst)
fprintf(' Default transition -> %s\n', dstStr);
elseif ~isempty(src) && ~isempty(dst)
fprintf(' Transition: %s -> %s\n', srcStr, dstStr);
elseif ~isempty(src) && isempty(dst)
fprintf(' Dangling transition from %s -> [NONE]\n', srcStr);
else
fprintf(' Orphan transition [NO SOURCE] -> [NO DEST]\n');
end
end
end
Output:
Default transition -> State:Init
Transition: State:wait -> State:MAIN
Default transition -> State:wait
Dangling transition from State: -> [NONE]
Default transition -> State:check
Transition: State:check -> State:wait
Default transition -> State:idle
Transition: State:idle -> State:running
Refer to following resources to explore more about the same:
- Default Transitions: https://www.mathworks.com/help/stateflow/api/stateflow.chart.defaulttransitions.html
- Access objects in stateflow chart: https://www.mathworks.com/help/stateflow/api/accessing-existing-stateflow-objects.html
Hope this helps you!
0 Comments
See Also
Categories
Find more on Simulink Functions 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!