How to iterate through the ports of a block/model

10 visualizzazioni (ultimi 30 giorni)
Kevin
Kevin il 1 Giu 2011
Risposto: Gaganjyoti Baishya il 21 Giu 2020
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 Commenti
Fangjun Jiang
Fangjun Jiang il 1 Giu 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?
Kevin
Kevin il 1 Giu 2011
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

Accedi per commentare.

Risposte (3)

Arnaud Miege
Arnaud Miege il 1 Giu 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 Commenti
Arnaud Miege
Arnaud Miege il 1 Giu 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.
Kevin
Kevin il 2 Giu 2011
Sorry - I should have clarified I was running from a script.

Accedi per commentare.


Fangjun Jiang
Fangjun Jiang il 1 Giu 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.
  1 Commento
Kevin
Kevin il 2 Giu 2011
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

Accedi per commentare.


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

Categorie

Scopri di più su Programmatic Model Editing in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by