MATLAB: Reorder inports of masked subsystem dynamically

masksimulink

Hi,
I have implemented a masked subsystem with a variable number of Inports by replacing unneeded inputs by Ground blocks. The connections to the inner blocks of the subsystem remain intact. I outline the contents of the subsystem below. The inports are labeled by their name followed by the port index in parenthesis which denotes the order of appearance to the outside of the block.
|----------------|
In1(1) -----> |:1 |
In2(2) -----> |:2 Inner block |
In3(3) -----> |:3 |
In4(4) -----> |:4 |
|----------------|
Assuming In1 is not needed the subsystem looks like this:
|----------------|
Ground -----> |:1 |
In2(1) -----> |:2 Inner block |
In3(2) -----> |:3 |
In4(3) -----> |:4 |
|----------------|
All fine, however when I re-enable the disabled port the index of In1 is obviously wrong because the newly added inport assumes the next free port index.
|----------------|
In1(4) -----> |:1 |
In2(1) -----> |:2 Inner block |
In3(2) -----> |:3 |
In4(3) -----> |:4 |
|----------------|
To avoid confusion I would like to restore the original port indices to match the names like in the very first figure.
I tried to use
set_param('In1', 'Port', '1');
but get a "??? Invalid Simulink object specifier." error.
Thanks for all replies!

Best Answer

Hi,
just wanted to say that I solved my problem. In fact Lucas answer gave a valuable hint. In fact I had the full path to the inport as indicated by the observation that I was able to get_param() the 'Port' parameter. However, the set_param() function apparently expects the port path as a string argument while I was using a one-dimensional cell-array returned by find_system().
Also, you must not set a Port number larger than the actual number of inports in the subsystem, because this at least screws up the display and possibly more.
All I all, this is what works for me:
function [] = ShowInPort(name, show, port)
% find block of given name in current subsystem
block = find_system(gcb, ...
'LookUnderMasks', 'on', ...
'FollowLinks', 'on', ...
'Name', name);
if isempty(block)
error('No block named "%s" found in subsystem', name);
end
if show
if strcmp(get_param(block, 'BlockType'), 'Ground')
replace_block(gcb, ...
'LookUnderMasks', 'on', ...
'FollowLinks', 'on', ...
'Name', name, 'built-in/Inport', 'noprompt');
end
% get a list of Inports
inports = find_system(gcb, ...
'LookUnderMasks', 'on', ...
'FollowLinks', 'on', ...
'BlockType', 'Inport');
% when changing the port number, be sure to never set a
% port number larger than the actual number of inports present
% in the subsystem
set_param(cell2mat(block), 'Port', num2str(min(port, length(inports))));
else
if strcmp(get_param(block, 'BlockType'), 'Inport')
replace_block(gcb, ...
'LookUnderMasks', 'on', ...
'FollowLinks', 'on', ...
'Name', name, 'built-in/Ground', 'noprompt')
end
end
Thanks anyway.
Related Question