[matlab]Auto-connect block to existing line

来源:互联网 发布:网络借贷 暂行办法 编辑:程序博客网 时间:2024/06/05 23:00

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



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')%% setupadd_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 numberdelete_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')

原创粉丝点击