Search code examples
matlabsimulink

Simulink: add block in a for loop from script to the model


Through a script I am trying to create smaller moduls from the whole and complex model. I cut this modul and add it in a new model. That works.

In second step what I want to do is to add constant blocks according the number of the inputs. For the outputs I want to add terminator according the number of the outputs. Sure, in the end they have to be connected.

To add constant block I use the following command (it works):

add_block('simulink/Commonly Used Blocks/Constant', 'my_model/Constant1')

This has to work in a for-loop according that how many inputs exist.

To connect this constant blocks with the input (doenst work):

add_line('my_model', 'Constant1', 'Input1')

How can I achieve this in a for loop?


Solution

  • I find a solution like below. It can add blocks and connect it

    for i = 1:5; 
       add_block('simulink/Commonly Used Blocks/Constant', strcat('myModel/Input',num2str(i)));    
       add_line('myModel',strcat('Input',num2str(i),'/1'), strcat('block_name/',num2str(i)));
    end