How to iterate through the ports of a block/model

6 次查看(过去 30 天)
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 个评论
Fangjun Jiang
Fangjun Jiang 2011-6-1
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?
Kevin
Kevin 2011-6-1
1. Not likely
2. Yes, blocks which are subsystems, as well as external models themselves.
3. Sure - shouldn't all access to any blocks use the same method?
Thanks,
-Kevin

请先登录,再进行评论。

回答(3 个)

Arnaud Miege
Arnaud Miege 2011-6-1
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 个评论
Arnaud Miege
Arnaud Miege 2011-6-1
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.
Kevin
Kevin 2011-6-2
Sorry - I should have clarified I was running from a script.

请先登录,再进行评论。


Fangjun Jiang
Fangjun Jiang 2011-6-1
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.
  1 个评论
Kevin
Kevin 2011-6-2
Awesome - this worked perfectly. The other examples returned multiple references from find_system(). I assume the SearchDepth 1 eliminates sub-blocks from the top-level.
Thanks to everyone for the assistance!
-Kevin

请先登录,再进行评论。


Gaganjyoti Baishya
Gaganjyoti Baishya 2020-6-21
You can get the number of ports in a model by get_param(gcb, 'PortHandles')

类别

Help CenterFile Exchange 中查找有关 Programmatic Model Editing 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by