MATLAB Answers

How do I get the port names of a block based on their port handles?

75 views (last 30 days)
Kulan Sinclair
Kulan Sinclair on 11 Jul 2018
Edited: Simon Ellwanger on 26 Feb 2021
I'm trying to find A,B,C,a,b or c from the port handle they are set from inside the mask but if I try something like
get_param(porthandle, 'name')
I get an empty string, but name is a valid object parameter.

Answers (2)

Christopher Wallace
Christopher Wallace on 19 Jul 2018
Select the subsystem and then use the following code:
find_system(gcb, 'SearchDepth', 1, 'BlockType', 'Inport')
find_system(gcb, 'SearchDepth', 1, 'BlockType', 'Outport')
  1 Comment
Matthew Calder
Matthew Calder on 4 Aug 2020
Hi, do you know the criteria you need to search for to find a simscape PMC_Port? in place of the 'Inport' or 'Outport'. I have tried, PMCPort, PMC_Port and Port but they dont seem to work...
Thanks!

Sign in to comment.


Simon Ellwanger
Simon Ellwanger on 26 Feb 2021
Edited: Simon Ellwanger on 26 Feb 2021
try this:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% please load and select/mark the block before execution !!!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%get block handles and port handles
rootmodel= gcb;
porthandles=get(gcbh,'PortHandles')
% get OUTPORT PortNames:
simBlockH = get_param(gcbh, 'Handle')
handles = find_system(simBlockH, 'LookUnderMasks', 'on', 'FollowLinks', 'on', 'SearchDepth', 1, 'BlockType', 'Outport');
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

Community Treasure Hunt

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

Start Hunting!