Removing a block and making connections

19 vues (au cours des 30 derniers jours)
Michael Joslin
Michael Joslin le 20 Fév 2012
Commenté : Marco Baratelli le 22 Jan 2016
Lets say I have a simple subsystem that is just an inport, an outport and a gain block. I want to get rid of the gain block and just have the inport connected to the outport. If i use replace_block I can only replace the gain with another block and if I use delete_block it will remove the gain but leave two disconnected lines. How can I remove this block and make the connection between the remaining two using code?

Réponse acceptée

Jarrod Rivituso
Jarrod Rivituso le 20 Fév 2012
I've run into this before. My solution was to find port handles and reconnect. I'm not sure if there is an easier way.
Here's a function that should do the trick:
function removeAndReconnectBlock(blockHandle)
%Get port handles and parent system
portHandles = get_param(blockHandle,'PortHandles');
sys = get_param(blockHandle,'Parent');
%Get source port
srcSignal = get_param(portHandles.Inport,'Line');
srcPort = get_param(srcSignal,'SrcPortHandle');
%Get destination port
destSignal = get_param(portHandles.Outport,'Line');
destPort = get_param(destSignal,'DstPortHandle');
%Remove
delete_line(destSignal);
delete_line(srcSignal);
delete_block(blockHandle);
%Reconnect
add_line(sys,srcPort,destPort)
Note that this only works for single input, single output blocks, but I'm sure you could see how you would extend it further.
  1 commentaire
Michael Joslin
Michael Joslin le 20 Fév 2012
This really help, thanks a lot!

Connectez-vous pour commenter.

Plus de réponses (1)

chandu
chandu le 8 Déc 2014
May I know how to disconnect SimPowerSystems blocks using PortHandles? Please help me ASAP.....
  1 commentaire
Marco Baratelli
Marco Baratelli le 22 Jan 2016
The following works for me using SimScape! Hope it helps.
sbh = getSimulinkBlockHandle([sys '/NameOfYourSpecificBlock']);
ph = get_param(sbh,'PortHandles');
srcSignal = get_param(ph.RConn,'Line'); % or LConn for left connection of the block
delete_line(srcSignal);

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programmatic Model Editing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by