Simulink get name/label of a port programmatically
179 views (last 30 days)
Show older comments
In my model I've a Delay block (see image below) connected with other blocks (not shown in the image). I have handles of each of the three ports of the Delay block, but I do not know which handle belongs to which port. Is there a programmatic way to get the "name" of the ports (e.g. Getting the values u, d and x0)?
I have access to the block's handle and handles of the three ports as well.
Please note that the labels u, d and x0 as shown in the picture are provided by Simulink, not me. And I do not want to put custom labels/tags to the ports.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/160718/image.png)
0 Comments
Answers (4)
Simon Ellwanger
on 26 Feb 2021
Edited: Simon Ellwanger
on 26 Feb 2021
try this:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% please open in Simulink and select/mark the block before execution !!!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%get block handles and port handles
rootmodel= gcb;
porthandles=get(gcbh,'PortHandles')
% get INPORT PortNames (change to OUTPORT if needed):
simBlockH = get_param(gcbh, 'Handle')
handles = find_system(simBlockH, 'LookUnderMasks', 'on', 'FollowLinks', 'on', 'SearchDepth', 1, 'BlockType', 'Inport');
portInfo = [get_param(handles, 'Name'), get_param(handles, 'Port')]
for i = 1: size(porthandles.Outport,2)
%draw line at each port
pos = get(porthandles.Outport(i), 'Position');
Linepos = [pos(1)+200 pos(2)-5 pos(1)+200+10 pos(2)+5];
temp_block = add_block('simulink/Commonly Used Blocks/Terminator',[gcs '/Term_' int2str(i)],'Position', Linepos);
h1 = get_param(temp_block,'PortHandles');
add_line(gcs,porthandles.Outport(i),h1.Inport);
% name line
linehandle = get(porthandles.Outport(i), 'Line');
set(linehandle, 'Name', portInfo{i});
end
0 Comments
Zoe Xiao
on 2 Aug 2017
The labels shown on the block is not the name of the port. By default, the name of a port is empty.
I believe you've used 'get_param' to obtained the port handles of this block. The results categorize the port handles based on the types of the ports. For the same type, the handles are ordered based on the ports' position. It should be from left-to-right or up-to-bottom.
In the case of this 'delay' block, it is expected to have three handles listed in the 'Inport' field, which are corresponding to port 'u', 'd', 'x0', respective. If you want to mark them for future reference, you could give them names with 'set_param(PortHandle,'Name', name)'
1 Comment
Sylvain R.
on 19 Feb 2018
Edited: Sylvain R.
on 19 Feb 2018
Try searching within the block the list of inports / outports using find_in_models. It should look like:
find_in_models( ...
yourBlockHandle, ...
'SearchDepth', '1', ...
'BlockType', 'Inport');
Look at the subroutine find_system (used in find_in_models) for more options on the search.
Luckily, ports are ordered in the way they are numbered (could find no evidence for this).
0 Comments
See Also
Categories
Find more on Programmatic Model Editing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!