Simulink get name/label of a port programmatically

137 Ansichten (letzte 30 Tage)
giga
giga am 14 Feb. 2017
Bearbeitet: Simon Ellwanger am 26 Feb. 2021
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.

Antworten (4)

Simon Ellwanger
Simon Ellwanger am 26 Feb. 2021
Bearbeitet: Simon Ellwanger am 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

Zoe Xiao
Zoe Xiao am 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 Kommentar
GMM
GMM am 25 Jan. 2018
Bearbeitet: GMM am 25 Jan. 2018
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.

Melden Sie sich an, um zu kommentieren.


Sylvain R.
Sylvain R. am 19 Feb. 2018
Bearbeitet: Sylvain R. am 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).

Ryan Jones
Ryan Jones am 24 Jul. 2019
Same Question

Kategorien

Mehr zu Programmatic Model Editing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by