Simulink get name/label of a port programmatically
137 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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.
0 comentarios
Respuestas (4)
Simon Ellwanger
el 26 de Feb. de 2021
Editada: Simon Ellwanger
el 26 de Feb. de 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 comentarios
Zoe Xiao
el 2 de Ag. de 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 comentario
Sylvain R.
el 19 de Feb. de 2018
Editada: Sylvain R.
el 19 de Feb. de 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 comentarios
Ver también
Categorías
Más información sobre Programmatic Model Editing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!