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?
John Williams answered .
2025-11-20
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')