Auto-connect block to existing line

24 views (last 30 days)
I want to add in some data type conversion blocks infront of some already existing inport blocks. I can create the block and add them to the model and they appear on the port line where I expect them, but they do not connect to that line. They appear above the line and I have to drag them to get them attached to the line. So I was wondering if there was any way I could connect the new block to the line that’s already there from the command line.

Accepted Answer

Albert Yam
Albert Yam on 4 Sep 2012
Edited: Albert Yam on 4 Sep 2012
How are you "getting the old block out out the way"? I never figured out how to leave lines in place.
Last time I did something like this,
[in1]--line--[out1]
  1. %find_system out1 (or the list of what I was adding to)
  2. get the position of out1 = pos
  3. get the input port to out1
  4. get the line handle
  5. get the source port (out port of in1)
  6. delete the line
  7. set_param out1 to position [pos(1)+dx pos(2) pos(3)+dx pos(4)]
  8. add conv block to 'pos'
  9. addline in1 to conv
  10. addline conv to out1
Side note, if the list is really long, name the blocks 'well' and do a loop at the end to add all the lines. My code, with lines added at every step, took 2-3 hours. Doing lines at the end, took 20 mins. [Might only apply to Simulink 6.6.1]
EDIT: Actually, for your specific question, have you tried (no idea if this will work). get_param the port location (x,y location)
add_line([x y], [x y]) %effectively, draw a 'dot'
or get the port location of the existing block, port location of the new block, and add_line to those
  1 Comment
Albert Yam
Albert Yam on 4 Sep 2012
I had no idea you could just drop a block on a line to connect it, that's cool. Learn something everyday. Unfortunately, I think that might just be a GUI feature.
Can you quasi-draw out what you want to add? (I think I just got it, by infront you mean behind?) So.
[inport]--->[subsys where there are 19 other inputs]
and you want?
[inport]-->[conv]-->[subsys]
Played around, got this (step through this)
new_system('simtest')
open_system('simtest')
simulink('open')
%%setup
add_block('simulink/Commonly Used Blocks/In1','simtest/in1')
set_param('simtest/in1','Position',[50 50 100 75])
add_block('simulink/Commonly Used Blocks/Out1','simtest/out1')
set_param('simtest/out1','Position',[500 50 550 75])
add_line('simtest','in1/1','out1/1')
%%real code
%find_system blah blah in1
pos1 = get_param('simtest/in1','Position');
pos2 = [pos1(1)+200 pos1(2) pos1(3)+200 pos1(4)];
set_param('simtest/in1','Position', pos2);
%get a lot of parameters here for in1, name, port number
delete_block('simtest/in1')
add_block('simulink/Commonly Used Blocks/Data Type Conversion','simtest/conv1')
set_param('simtest/conv1','Position', pos2);
%set_param('OutDataType')
add_block('simulink/Commonly Used Blocks/In1','simtest/in1')
set_param('simtest/in1','Position', pos1);
%set_param( name, port number)
add_line('simtest','in1/1','conv1/1')

Sign in to comment.

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 4 Sep 2012
Edited: Azzi Abdelmalek on 4 Sep 2012
% the line fom you want connect to your new block "new_block" is an output for a certain block "block1"
%for example new_block is a "constant" block and modelname is the name of your simulink model
add_blck('Simulink/Sources','modelname/new_block')
% to position your block
set_param('modelname/new_block','position' , [x0 y0 x0+x_width y0+y_width])
%connect block
add_line('modelname','block1/1','new_block/1','autorouting','on')
% 'block1/n' means n'th output of block1
% 'new_block/n' means n'th input of new_block

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!