主要内容

simscape.connectedPorts

Get information about connected ports

Since R2026a

    Description

    ports = simscape.connectedPorts(block,portName) returns a structure array of the block and port names connected to the specified port, portName, of the block, block.

    example

    Examples

    collapse all

    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.

    Model diagram with connected blocks

    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]);
    

    Model diagram with Inductor block added

    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")
    

    Model diagram with removed connection

    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)
    

    Model diagram with connection rerouted

    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")
    

    Model diagram with circuit connections completed

    Input Arguments

    collapse all

    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

    collapse all

    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