Programatically create a new Stateflow chart inside an existing subsystem

25 views (last 30 days)
The following concerns versions 2020b and 2024b.
I'm creating a code importer that parses XML to generate a model within simulink. The XML encodes regular interconnected logic gates, numerical comparitors, conditionals, etc arranged in subsystems. This all works fine. One subsystem contains a Stateflow chart and this is where I fall down.
I can create a new sf chart using the sfnew command, but this creates a new chart in a new window. I can't see anyway of telling it to create a chart object in a defined subsystem. I also can't see a way of even telling sfnew to create a new chart silently, without spawning a new window. I want to do all this in memory withou windows popping in and out of existance.
I have tried commands similar to the following,
>> modelName = 'flow_demo';
>> open_system(modelName);
>> subsystemPath = [modelName,'/Subsystem'];
>> add_block('built-in/Subsystem', subsystemPath);
>> subsystemHandle = get_param(subsystemPath, 'Handle');
>> chart = Stateflow.Chart(subsystemHandle);
but I get the following error message:
Error using Stateflow.Chart
No constructor 'Stateflow.Chart' with matching signature found.
Out of despiration I also tried add_block, but met with the error 'There is no block named 'Stateflow/Chart''
So how can I create a new stateflow chart silently, similar to 'add_block('built-in/Subsystem', subsystemPath);'?
After this point the APIs allow me to manipulate the the chart as I need.
Thanks.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 23 Oct 2025 at 11:54
Use below to add a new Stateflow chart block and then use Stateflow API.
add_block('sflib/Chart','MyModel/MySubSystem/MyChart')
Usually, start with below to find the handle of the Stateflow chart
ch = find(sfroot,"-isa","Stateflow.Chart")

More Answers (0)

Categories

Find more on Simulink Functions in Help Center and File Exchange

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!