How to iterate through the ports of a block/model
6 views (last 30 days)
Show older comments
How do I find the number of ports and then get the port name for each port? I found several answers, similar to: <http://www.mathworks.de/matlabcentral/newsreader/search_results?dur=all&page=1&search_string=porthandles&view=text>
But these answers use: lcPortHandles = get_param('foo_block', 'PortHandles');, which results in: ??? block_diagram does not have a parameter named 'PortHandles'.
What gives? According to http://www.mathworks.com/help/toolbox/simulink/slref/f23-7517.html, which lists the PortHandles attribute, "This table lists the parameters common to all Simulink blocks". I have also noticed the documentation lists all of the parameters using set_param, but doesn't say they are able to be read using get_param.
Anyhoo - I am a bit confused. I need to get the port names of the blocks, since many of the commands require the port name in order to run them.
Thanks,
-Kevin
2 Comments
Fangjun Jiang
on 1 Jun 2011
What ports and port names are you looking for?
1. Inport blocks or Outport blocks at the root level of the model?
2. The Inport and Outport of a subsystem block?
3. The Inport and Outport of any other blocks?
Answers (3)
Arnaud Miege
on 1 Jun 2011
Select a block and then use:
get_param(gcb,'PortHandles')
Here's an example when selecting a mux block with one input port and one output port
>> temp = get_param(gcb,'PortHandles')
temp =
Inport: 1.9250e+003
Outport: [1.9260e+003 1.9270e+003]
Enable: []
Trigger: []
State: []
LConn: []
RConn: []
Ifaction: []
This tells me that this block has one input port and two output ports, since temp.Inport is of dimension 1 and temp.Outport is of dimension 2. The handles to the ports are given by temp.Inport and temp.Outport.
Instead of using gcb you can enter the path of the block, e.g. sldemo_autotrans/ThresholdCalculation/interp_up.
HTH,
Arnaud
4 Comments
Arnaud Miege
on 1 Jun 2011
PS: you didn't say in your question you were doing this from a script.
blks = find_system(<model_name>, 'Type', 'block') will give you all the blocks in the model, regardless of whether they are regular blocks, subsystems or referenced models.
blks = find_system(<model_name>, 'BlockType', 'SubSystem') will give you all the subsystems in the model
blks = find_system(<model_name>, 'BlockType', 'ModelReference') will give you all the referenced models in the model.
Fangjun Jiang
on 1 Jun 2011
To find out the Inport and Outport of a subsystem block:
handles=find_system('MyModel/MySubsystemBlockName','FindAll','On','SearchDepth',1,'BlockType','Inport'); InportNames=get(handles,'Name');
To find out the port information of other blocks, follow Arnaud's post. Note that usually those ports don't have a name. That's why I ask those questions on your original question.
Gaganjyoti Baishya
on 21 Jun 2020
You can get the number of ports in a model by get_param(gcb, 'PortHandles')
0 Comments
See Also
Categories
Find more on Programmatic Model Editing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!