Simulink get name/label of a port programmatically

189 vues (au cours des 30 derniers jours)
giga
giga le 14 Fév 2017
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.

Réponses (4)

Simon Ellwanger
Simon Ellwanger le 26 Fév 2021
Modifié(e) : Simon Ellwanger le 26 Fév 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 le 2 Août 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 commentaire
GMM
GMM le 25 Jan 2018
Modifié(e) : GMM le 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.

Connectez-vous pour commenter.


Sylvain R.
Sylvain R. le 19 Fév 2018
Modifié(e) : Sylvain R. le 19 Fév 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 le 24 Juil 2019
Same Question

Catégories

En savoir plus sur Programmatic Model Editing dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by