Simulink add a line between ports programatically

8 views (last 30 days)
Hi,
I want to add a line between two ports in Simulink (Simscape blocks). I am using add_line to achieve that. The issue I have is that in some cases I get an error "The second port already has a line connection". I have tried using the syntax with position as well as with ports. This somehow makes sense, what I want to achieve is to be able to detect scenarios where the line already exists and still make a connection between the lines connected to the ports. I have not found a way to do that. Normally when I use the add_line with the positions of the ports, I do not see this exception, nonetheless the new line is not added (rather you see it in red). Do you have any ideas?

Answers (1)

Fangjun Jiang
Fangjun Jiang on 20 Apr 2020
A line has a source port and a destination port. At the source port, the port can have multiple lines. But at the destination port, you can't have multiple lines. (A signal can go to multiple destinations but a signal can only have one source). So to make your script robust, you have to check first whether the destination port already has a line handle. If it does, delete that line first and then do add_line().
  9 Comments
Fangjun Jiang
Fangjun Jiang on 30 Apr 2020
I dropped a Battery, Resistor, Resistor1 to a model and ran below. I don't see the problem even if I put a red line there first manually.
add_line(model,'Battery/LConn1','Resistor/LConn1')
add_line(model,'Battery/RConn1','Resistor/RConn1')
add_line(model,'Resistor1/RConn1','Resistor/RConn1')
add_line(model,'Resistor1/LConn1','Resistor/LConn1')
add_line(model,'Resistor1/LConn1','Resistor/RConn1')
Sergey Kasyanov
Sergey Kasyanov on 3 Mar 2021
Hello, @Iliya Valchev. Did you find the way to robust connection of two and more ports?

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!