How to connect a simscape block with add_line?

17 次查看(过去 30 天)
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

采纳的回答

Steve Miller
Steve Miller 2015-1-21
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 个评论
Francisco Williams
Francisco Williams 2021-1-29
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
Francisco Williams
Francisco Williams 2021-1-29
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)

请先登录,再进行评论。

更多回答(1 个)

Thomas Knapp
Thomas Knapp 2015-1-21
Hi Steve,
thank you very much! Now everything works fine :)
Thomas

类别

Help CenterFile Exchange 中查找有关 Programmatic Model Editing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by