MATLAB: Conditional Simulink Block Deletion

delete_blockdelete_lineget_paramlinehandlesMATLABportconnectivitysimulink

The aim of the code is to scour a .mdl and delete any mux blocks that are not connected to anything (outport) and then their input signals on that condition. On a side note I am a massive noob.
Start by determining the paths of all the mux blocks:
mux_list = find_system(model_name, 'LookUnderMasks', 'all', 'BlockType', 'Mux')
number_of_mux = size(mux_list);
number_of_mux = number_of_mux(1);
Then proceed with the operation. The operation successfully deletes mux blocks who's outputs are not connected to anything. However, it also deletes mux blocks whose outports are connected to masked blocks (rather than standard blocks) – this is undesirable and is the issue.
if number_of_mux == 0
disp('No Mux detected')
else
muxies=num2cell(zeros(number_of_mux,1)); % memory preallocation.
finalno = number_of_mux
i = 1
for i=1:1:number_of_mux
muxies(i,:)=mux_list(i);
mux_current = char(mux_list(i))
linehandle_current = get_param(mux_current, 'LineHandles');
signal_current = linehandle_current.Inport;
lolcats=get_param(mux_current, 'PortConnectivity')
lolcats=lolcats.DstBlock
deletequery = isempty(lolcats)
if deletequery == 1
delete_line(signal_current);
delete_block(mux_current);
mux_del_list{:,i}=mux_current;
else
finalno = finalno - 1
end
end
disp([num2str(finalno), ' mux(s) of ', num2str(number_of_mux), ' were found and deleted. '])
disp(char(mux_del_list))
end
I presume I need some sort of paradigm shift in my approach to this to get it working as expected.
Best regards,
Brown

Best Answer

Try
mux_list = find_system('test2', 'LookUnderMasks', 'all', 'BlockType', 'Mux');
number_of_mux = size(mux_list);
number_of_mux = number_of_mux(1);
if number_of_mux == 0
disp('No Mux detected')
else
muxies=num2cell(zeros(number_of_mux,1)); % memory preallocation.
finalno = number_of_mux;
i = 1;
for i=1:1:number_of_mux
muxies(i,:)=mux_list(i);
mux_current = char(mux_list(i));
linehandle_current = get_param(mux_current, 'LineHandles');
signal_current = linehandle_current.Inport;
% lolcats=get_param(mux_current, 'PortConnectivity')
% lolcats=lolcats.DstBlock
% deletequery = isempty(lolcats)
deletequery = linehandle_current.Outport == -1; %%ONLY CHANGE
if deletequery == 1
delete_line(signal_current);
delete_block(mux_current);
mux_del_list{:,i}=mux_current;
else
finalno = finalno - 1;
end
end
disp([num2str(finalno), ' mux(s) of ', num2str(number_of_mux), ' were found and deleted. '])
disp(char(mux_del_list))
end