How to connect a simscape block with add_line?

Hey there, I want to connect a PMC port in SimMechanics automatically with a Matlab-Script. Using the 'add-line' command works fine for common simulink-connections, but not for Simscape-ports. For example if I want to connect the block 'World Frame' with 'Rigid Transform' I use
% code
add_line('mysystem','World Frame/1','Rigid Transform/1')
But then I always get the message:
Invalid Simulink object name: World Frame/1
Does anbody know how to connect these ports in a good way?
Thanks in advance, Thomas

 Respuesta aceptada

Steve Miller
Steve Miller el 21 de En. de 2015

2 votos

Hi Thomas,
add_line() works with Simscape ports. You have to refer to the ports differently. Try this command:
add_line('mysystem','World Frame/RConn1','Rigid Transform/LConn1');
There is a submission on the MATLAB Central File Exchange that shows how to use MATLAB to automate the construction of a SimMechanics model. The submission "SimMechanics Model from MATLAB Code" shows how to add blocks and connect them using MATLAB commands. The same commands work for Simscape blocks, too.
Hope this helps! --Steve

2 comentarios

Hi,
I know this is an old question but I can't find a newer thread.
I'm having the same problem with Matlab2020b. Is there a way to query the names of the blocks?
I created a Subsystem (With Simscape multibody blocks inside) and I want to connect it to another block. But I can't manage to get the proper name of the ports.
Thanks in advance
Oh I found a workaround for this:
% Select the first subsystem
>>h1 = get_param(gcb,'PortHandles')
struct with fields:
Inport: []
Outport: []
Enable: []
Trigger: []
State: []
LConn: 333.0016
RConn: []
Ifaction: []
Reset: []
% Select the second subsystem
>>h2 = get_param(gcb,'PortHandles')
struct with fields:
Inport: []
Outport: []
Enable: []
Trigger: []
State: []
LConn: 572.0040
RConn: 974.0093
Ifaction: []
Reset: []
% Or instead of entering gcb, enter the path of the subsystem. The first
% subsystem has only 1 output port and the second one 2
% add the connection between blocks with:
add_line(gcs,port_h1.LConn,port_h2.LConn)

Iniciar sesión para comentar.

Más respuestas (1)

Thomas Knapp
Thomas Knapp el 21 de En. de 2015

0 votos

Hi Steve,
thank you very much! Now everything works fine :)
Thomas

Categorías

Preguntada:

el 20 de En. de 2015

Comentada:

el 29 de En. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by