MATLAB: Do I receive a “Error using Stateflow.​Clipboard/​copy” when I try to update Stateflow library interfaces that contain graphical Stateflow functions that are set to “subchart” in R2017a+

stateflow

I am running a MATLAB script to update Stateflow library interfaces on a library that contains a graphical function set to be a "subchart". In this script, I create a "sfclipboard" object, grab the content from the current library, copy the content to the clipboard, and paste this to the new system that gets created by the script.
When I run the script on my library that contains a graphical function set to be a "subchart", I receive the following error message:
Error using Stateflow.Clipboard/copy
All objects must have the same subviewer
Error in update_library (line 168)
Clipboard.copy(AllObjects);
This seems like strange behavior since the subchart should be able to be copied without running into any issues through the clipboard object.

Best Answer

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:
% Find the matching chart. Get contents.
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:
% Find all objects
states = OldChart(iOldChart).find('-isa', 'Stateflow.State');
% Select the objects that are in the same subviewer
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.