Reverting Referenced Subsystems back to normal Subsystems

51 views (last 30 days)
Hi,
I use MATLAB/Simulink R2019b and its new feature: Subsystems Reference.
During develpoment I use referenced subsystems since they are used in multiple models.
Before the models are distributed to customers, the referenced subsystems must be reverted back to 'normal' subsystems for various reasons.
Since MathWorks does not provide a feature for this behavior I wrote some code in order to do it. In this code I use the 'expand subsystem' feature to break the referenced subsystem links.
Now I have to change my MATLAB/Simulink version to R2020a and suddenly the expand subsystem feature no longer works for referenced subsystems. This change is not documented in the release notes or anywhere else.
Has anyone an idea how I can change my referenced subsystems back to 'normal' subsystems without copying the whole content from one subsystem into antoher one?

Accepted Answer

Asvin Kumar
Asvin Kumar on 18 May 2021
Hi Marvin,
I can confirm that the feature to expand referenced subsystems was removed in R2020a. So, it looks like the best way around is to copy the referenced subsystem to all locations. This can be done using the simulink.blockdiagram.copycontentstosubsystem command. I suppose replace_block will be useful along with it but I haven't tried it out.
I've written a script to demonstrate how you could go about this. I've used add_line and delete_line instead to reconnect the subsystems.The models I have used to test this script are attached.
Here's the original subsystem:
Here's the same subsystem referenced in the model:
Here's what it looks like after replacement:
And here's the script that does that:
refSubsystem = 'subsys20b';
modelName = 'modelRefSubsys20b';
open_system(modelName);
oldBlock = [ modelName '/Subsystem Reference'];
newBlock = [ modelName '/subsystem'];
% create a copy of the referenced subsystem
add_block('built-in/Subsystem', newBlock)
Simulink.BlockDiagram.copyContentsToSubsystem(refSubsystem, newBlock)
% Retrieve useful info from the old block
t1 = get_param(oldBlock, 'Portconnectivity');
t2 = get_param(oldBlock, 'PortHandles');
t3 = get_param(newBlock, 'PortHandles');
% delete old lines
h = get_param(oldBlock, 'LineHandles');
ni = numel(h.Inport);
no = numel(h.Outport);
delete_line(h.Inport);
delete_line(h.Outport);
% create connections to new block
for i=1:numel(t2.Inport)
srcBkhdl = t1(i).SrcBlock; % source block handle
srcNm = getfullname(srcBkhdl); % source block name
srcBkPts = get_param(srcNm, 'PortHandles'); % source block port handles
srcPt = srcBkPts.Outport(t1(i).SrcPort+1); % source port
dstPt = t3.Inport(i); % destination port
add_line(modelName, srcPt, dstPt);
end
% create connections from new block
for i=1:numel(t2.Outport)
dstBkhdl = t1(ni+i).DstBlock; % dest block handle
dstNm = getfullname(dstBkhdl); % dest block name
dstBkPts = get_param(dstNm, 'PortHandles'); % dest block port handles
dstPt = dstBkPts.Inport(t1(ni+i).DstPort+1); % dest port
srcPt = t3.Outport(i); % src port
add_line(modelName, srcPt, dstPt);
end
% copy to same position
pos = get_param(oldBlock, 'position');
set_param(newBlock, 'position', pos);
% remove old block
delete_block(oldBlock);
% save changes
save_system(modelName);
% replace_block might work.. Haven't tried
% replace_block(modelName, 'Subsystem Reference', 'subsystem')
Some notes:
I do not regularly edit models programmatically. So, my approach might be inefficient/roundabout. This is just a suggestion. You can use this as a reference to get started.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!