Add line between blocks using handles

Hi, I would like to connect blocks using their handles. Do you know if it's possible?
I know that add_line function uses ports but my script is a loop and when I add two same blocks, I use add_block adding (....,'MakeNameUnique','on'), hence I can't know block's name in order to call its own port in add_line.
What I am trying to do now is to get blocks handle in sequence through gcbh, from departure to arrival block, and I want to use handles in place of ports. Can anyone help me?

댓글 수: 1

Chiara
Chiara 2013년 5월 15일
편집: Chiara 2013년 5월 15일
path=getfullname(handle)
It returns 'sys/blockname'
Is there a way to have only blockname?
In add_line arguments I should add 'blockname/Portnum', shouldn't I?

댓글을 달려면 로그인하십시오.

답변 (2개)

Kaustubha Govind
Kaustubha Govind 2013년 5월 15일

1 개 추천

Instead of using gcbh, please use the return argument of add_block, which will be the handle to the newly created block. To get the name of the block, you can use get_param(handle, 'Name'), to get the full path to its parent, you can use get_param(handle, 'Parent').

댓글 수: 6

Chiara
Chiara 2013년 5월 21일
Hi, the problem is that when I add the same block by copying the one already existing, I can't know the name for the new block and I can't go to select the block in the model, cause It has to work automatically.
Kaustubha Govind
Kaustubha Govind 2013년 5월 21일
Chiara: I understand that you don't know the name of the new block. However, you can find out the name of the new block, by using the result of your add_block command in the get_param command that I suggested.
So, if I am not misunderstanding, should I write something similar?
h1=addblock(...);
h2=addblock(...):
path1=get_param(h1,'Parent');
path2=get_param(h2,'Parent');
port1=get_param(path1,'PortHandles');
port2=get_param(path2,'PortHandles');
add_line('sys', port1.RConn(1),port2.LConn(1));
you wrote: [ to get the full path to its parent, you can use get_param(handle, 'Parent') ]
I have just tried this function and I don't know if the result is the one expected.
I have added a block to a model 'new'. The block's path is new/source1. If I write:
get_param(add_block(),'Parent'),
It gives:
new
Is this path the correct full path to parent? I thought it was new/source1. Where am I wrong?
The parent is still only 'new', the block itself is named 'source1' in your case. You can construct the full path to the block by concatenating the two strings returned by get_param(h,'Parent') and get_param(h,'Name'). To add the line:
%Assuming both blocks have same parent
add_line(get_param(h1,'Parent'), ...
[get_param(h1,'Name') '/1'], ...
[get_param(h2,'Name') '/1']);
how to connect Trigger port with inport? Trigger port is not connecting with this method.

댓글을 달려면 로그인하십시오.

Jianguang Ban
Jianguang Ban 2019년 3월 14일
편집: Jianguang Ban 2019년 3월 14일

0 개 추천

For this question i wrote a simple script to show how to create a model and add some inputs and outputs, and add lines for each port to a specified block. this script was run with correct results:
ModelName = 'SpdCtrl';
new_system(ModelName);
open_system(ModelName);
component_dest_name = strcat(ModelName,'/',ModelName);
%% Create and add main block
topsystemblk = add_block('Simulink/Ports & Subsystems/Subsystem',component_dest_name,'position',[165 109 600 700]);
%% Create and add inporting blocks and lines from the dependencies of main component
Horizontal = 165;
Vertical = 150;
N = 200;
M = 165;
width = N - Horizontal;
height = M - Vertical;
vertical_shift = 0;
horizontal_shift = 0;
for i = 1:1:5
vertical_shift = 50*(i-1);
horizontal_shift = 100*(i-1);
inport_dest_name = strcat(component_dest_name,'/',strcat('In',num2str(i)));
inportblk = add_block('Simulink/Ports & Subsystems/In1',inport_dest_name,'position',[Horizontal Vertical+vertical_shift N M+vertical_shift]);
terminator_dest_name = strcat(component_dest_name,'/',strcat('Terminator',num2str(i)));
terminatorblk = add_block('Simulink/Sinks/Terminator',terminator_dest_name,'position',[Horizontal+100 Vertical+vertical_shift N+100 M+vertical_shift]);
% example: add_line(component_name,'In1/1','Terminator1/1');
add_line(component_dest_name,strcat('In',num2str(i),'/1'),strcat('Terminator',num2str(i),'/1'));
end

카테고리

도움말 센터File Exchange에서 Programmatic Model Editing에 대해 자세히 알아보기

제품

질문:

2013년 5월 15일

댓글:

2021년 10월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by