simscape.connectedPorts
Description
Examples
In this example, you create a new Simscape™ model, programmatically add electrical blocks and connect them using their
port names, and then programmatically disconnect one of the ports and reroute connections
to a port on another block. You use the simscape.connectedPorts
function to get information about the port connections in the model, and then use this
information to reroute the connections.
Create a new model, named MyModel.
sscnew("MyModel");
Add and position two Resistor blocks.
add_block("fl_lib/Electrical/Electrical Elements/Resistor","MyModel/Resistor1"); set_param("MyModel/Resistor1","position",[435,110,475,140]); add_block("fl_lib/Electrical/Electrical Elements/Resistor","MyModel/Resistor2"); set_param("MyModel/Resistor2","position",[435,186,475,214]);
Add an AC Voltage Source block and connect its negative port to the positive ports of the two resistors.
add_block("fl_lib/Electrical/Electrical Sources/AC Voltage Source","MyModel/Source"); simscape.addConnection("MyModel/Source","n","MyModel/Resistor1","p") simscape.addConnection("MyModel/Source","n","MyModel/Resistor2","p")
The model, with the connected blocks, looks like this.

Now, insert an Inductor block between the source and resistors and reroute the connections.
First, add and position the Inductor block.
add_block("fl_lib/Electrical/Electrical Elements/Inductor","MyModel/Inductor"); set_param("MyModel/Inductor","position",[340,71,380,99]);

Get information about the current connections for the negative port of the Source block.
connectedPorts = simscape.connectedPorts("MyModel/Source","n")
connectedPorts =
2×1 struct array with fields:
Block
PortName
To view information about the connected ports as a table, use the
struct2table function.
struct2table(connectedPorts)
ans =
2×2 table
Block PortName
___________________ ________
"MyModel/Resistor1" "p"
"MyModel/Resistor2" "p"
Disconnect the negative port of the Source block.
simscape.removeConnection("MyModel/Source","n")

Note that, when you disconnect the Source block, the two resistors are still connected.
Use the first element of the connectedPorts array to reroute the
connection to the negative port of the Inductor
block.
simscape.addConnection("MyModel/Inductor","n",connectedPorts(1).Block,connectedPorts(1).PortName)

Because the two resistors are still connected, you need to use only one element of
the connectedPorts array to restore the connection to both resistors.
To complete the circuit, add a connection from the negative port of the Source block to the positive port of the Inductor block.
simscape.addConnection("MyModel/Source","n","MyModel/Inductor","p")

Input Arguments
Block or subsystem, specified as a handle, a character vector, or string scalar. When you specify a character vector or string scalar, it must contain the full path to a block or subsystem in the model.
Data Types: double | char | string
Port name, specified as a character vector or string scalar. The port name can be
different than the port label on the model canvas. For more information, see simscape.connectionPortProperties. Use tab completion to list the valid
ports on the block.
Data Types: char | string
Output Arguments
Ports connected to the specified port, returned as a structure array of block and
port names. If the specified port is connected to only one port,
ports is a scalar structure, otherwise it is a structure array.
Each structure represents one connection and contains two fields: the
Block field is the name of the connected block, and the
PortName field is the name of the connected port on that block.
Version History
Introduced in R2026a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)