MATLAB: How to find duplicate sub systems in model using m-script

duplicate sub systeminputMATLABoutputsimulinksub systems

Hello,
I want to find duplicate sub systems in model. For example, I have sub system like shown image.
As we can see, duplicate sub system2 is created even though there is no IO changes.
so my interest is to find these kind issues in model using MATLAB commands.
Here is my code as of now:
subSystemHandles = find_system(bdroot,'BlockType','SubSystem');
for k=1 : length(subSystemHandles)
allSubSystem = get_param(subSystemHandles(k),'Name');
eachSubSystem = get(gcbh);
inPortHandles = eachSubSystem.LineHandles.Inport;
inPortNames=get(inPortHandles,'Name');
outPortHandles = eachSubSystem.LineHandles.Outport;
outPortNames=get(outPortHandles,'Name');
end
Here I am trying to read Names of Inport and OutPort using get_param but I am not getting names.
Please, Could some one help me on this?

Best Answer

This below code should work for reasonably simple subsystems. I haven't tried for any complex ones - like with triggered input.
mdl = 'MySubSys';
load_system('MySubSys')
allBlks = find_system(mdl, 'Type', 'block', 'BlockType', 'SubSystem');
flags = zeros(1,length(allBlks));
for k=1:length(allBlks)
currentPorts = get_param(allBlks{k}, 'Ports');
insideBlks = find_system(allBlks{k}, 'SearchDepth', 1, 'Type', 'block');
insideBlkType = get_param(insideBlks, 'BlockType');
insideIPnum = length(find(strcmp(insideBlkType, 'Inport')));
insideOPnum = length(find(strcmp(insideBlkType, 'Outport')));
insideSSNum = length(find(strcmp(insideBlkType, 'SubSystem')));
insideRestNum = length(insideBlkType)- insideIPnum - insideOPnum - insideSSNum;
if insideIPnum==currentPorts(1) && insideOPnum==currentPorts(2) ...
&& insideSSNum==2 && insideRestNum==0
flags(k)=1;
end
end
ids = find(flags(:)==1);
redundantSubSys = allBlks(ids);