Simulink get name/label of a port programmatically

179 次查看(过去 30 天)
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.

回答(4 个)

Simon Ellwanger
Simon Ellwanger 2021-2-26
编辑:Simon Ellwanger 2021-2-26
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

Zoe Xiao
Zoe Xiao 2017-8-2
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 个评论
GMM
GMM 2018-1-25
编辑:GMM 2018-1-25
Hello, I'm facing the same issues. Is there any way to get the labels that are shown in the block ports? I also need to do it programmatically. The number of ports and their label/name can change any time. Thanks.

请先登录,再进行评论。


Sylvain R.
Sylvain R. 2018-2-19
编辑:Sylvain R. 2018-2-19
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).

Ryan Jones
Ryan Jones 2019-7-24
Same Question

类别

Help CenterFile Exchange 中查找有关 Programmatic Model Editing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by