How to iterate through the ports of a block/model

조회 수: 10 (최근 30일)
Kevin
Kevin 2011년 6월 1일
답변: Gaganjyoti Baishya 2020년 6월 21일
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