How to programmatically rename Stateflow states that are grouped?

2 views (last 30 days)
From the command window I would like to rename all states that are in a Stateflow chart. I'll use the following built-in model as an example. In the chart there are two states, Off and On, that are grouped by the Heater box.
Screen Shot 2019-11-03 at 2.06.11 PM.png
I am receiving the error "Cannot change states inside a grouped state." when renaming the Off and On states because they are grouped by a box. If I simply double-click on the state name, I can easily rename via the usual Stateflow GUI, but I need to be able to do this in a script.
sys = 'sldemo_boiler';
rt = sfroot; % Get global Stateflow object
model = rt.find('-isa', 'Simulink.BlockDiagram', '-and', 'Name', sys); % Get the model
charts = model.find('-isa', 'Stateflow.Chart'); % Get the model's charts
c = charts(1); % The chart we are interested in
states = c.find('-isa', 'Stateflow.State'); % All states in the chart
state_off = states(1);
state_off.Name = 'NewName'; %% ERROR: Cannot change states inside a grouped state.
How can I rename these states? I considered just ungrouping the box by unsetting its IsGrouped parameter, but state objects don't have a parameter that tells me what object they are grouped by, so I can't trace up the hierarchy.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 4 Nov 2019
Edited: Fangjun Jiang on 4 Nov 2019
To handle this,
Box=c.find('Name', 'Heater');
To handle it more generically (without nested boxes)
%% Make needed changes
% recover the original flags
Monika Jaskolka
Monika Jaskolka on 5 Nov 2019
Thanks. My solution for now is:
% Save and turn off box grouping
boxes = c.find('-isa', 'Stateflow.Box');
isgrouped = cell2mat(get(boxes, 'IsGrouped'));
set(boxes, 'IsGrouped', 0);
states = c.find('-isa', 'Stateflow.State');
for m = 1:length(states)
% Make changes...
% Turn back on
for n = 1:length(boxes)
boxes(n).IsGrouped = isgrouped(n);

Sign in to comment.

More Answers (0)


Find more on Stateflow Programmatic Interface 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!