In MATLAB R2017a+, you can copy the content of the sub-charted graphical functions by setting the flag '-depth' to 1. This will copy a sub-charted function and its content all together. Please note that this solution works best if there is not a mix of sub-charted and non-sub-charted graphical functions and or states.
Please refer to the following lines of code below for an example of how to set this flag:
State = OldChart(iOldChart).find('-isa', 'Stateflow.State', '-depth', 1);
Transition = OldChart(iOldChart).find('-isa', 'Stateflow.Transition', '-depth', 1 );
Junction = OldChart(iOldChart).find('-isa', 'Stateflow.Junction', '-depth', 1);
Note = OldChart(iOldChart).find('-isa', 'Stateflow.Note', '-depth', 1);
Function = OldChart(iOldChart).find('-isa', 'Stateflow.Function', '-depth', 1);
Box = OldChart(iOldChart).find('-isa', 'Stateflow.Box', '-depth', 1);
Another alternative if there are some components that are not getting copied because they are in a different depth is to do the following:
states = OldChart(iOldChart).find('-isa', 'Stateflow.State');
states = states(arrayfun(@(x)(x.Subviewer.Id == x.Chart.Id), states));
Then, you should be able to use the "sfclipboard" object to copy and paste the contents of the library.
Best Answer