Simulink add a line between ports programatically

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?

Respuestas (1)

Fangjun Jiang
Fangjun Jiang el 20 de Abr. de 2020

0 votos

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 comentarios

Iliya Valchev
Iliya Valchev el 21 de Abr. de 2020
This makes sense. I am using Simscape blocks and I want to connect the LConn and RConn ports and I do not want to delete the existing connection/lines. What I want to achieve is to connect the line from one of the ports to the line of the other, due to the error that I have described this does not always work and I have not found a robust way to connect the lines (using the position of the ports does not always work, but in this case there is no error, rather red-unconnected lines)
Fangjun Jiang
Fangjun Jiang el 21 de Abr. de 2020
I've done an example connecting two red dotted lines together. Maybe you can make this approach work for your case.
Get the line handles of these two existing red dotted line, get the "Points" from line property, set the end point of the first line to be the same as the start point of the second line, the two lines are connected.
Iliya Valchev
Iliya Valchev el 30 de Abr. de 2020
The issue is that I don't know when the lines would not connect (so the non-connected red line appears). I need a robust way to connect the ports together, or let's say the line from the first port to the line of the second port
Fangjun Jiang
Fangjun Jiang el 30 de Abr. de 2020
So always check the second port first. If there is a line handle already, delete it or change it (maybe changing the source port of that line handle to be your first port will effectively add the line. I have not tried yet).
Iliya Valchev
Iliya Valchev el 30 de Abr. de 2020
I do not want to delete the line. Let's say, for whatever reason, I want to connect R9 - to R7 -. I do not want to delete the existing line on R7-, neither on R9-. I just want to make a new onnection between them
Fangjun Jiang
Fangjun Jiang el 30 de Abr. de 2020
Ah, physical modeling. All I said was for Simlink model (non physical model). I have not done programming on physical model.
Iliya Valchev
Iliya Valchev el 30 de Abr. de 2020
My question was not acurate, I have updated it
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')
Hello, @Iliya Valchev. Did you find the way to robust connection of two and more ports?

Iniciar sesión para comentar.

Categorías

Productos

Etiquetas

Preguntada:

el 20 de Abr. de 2020

Comentada:

el 3 de Mzo. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by