Real Technology Tools

MATLAB: Build a Simulink Model from a Script

In this article, you’ll see how to build a Simulink model from a MATLAB script. (You can find the complete script at the end of the article.) More specifically, you’ll learn to:

Open a Simulink Model from a MATLAB Script

First, click on “New Script” on the top left of MATLAB. This will create a new script by opening the MATLAB editor. Let’s start by saving that script and calling it “buildModel.m”.

Try running the script. Congratulations! You’ve successfully created your first model programmatically:
Model create from matlab automatically

Add a Block to Your Model

Create A Subsystem from MATLAB

Add/Remove Lines And Delete Blocks

Modify the Position of a Block

Build the Model

You now know everything you need about building a model from MATLAB, so let’s actually build it inside the subsystem.

Saving and Simulating the Model from MATLAB

Finally, if you double-click on the scope this is what you will get:

As you can see, this is a sinewave that has an average value of 1 because we’ve added 1 to the sinewave inside the subsystem.

Key takeaways:

  1. Create a model using the MATLAB command:
    open_system(new_system(modelName));
  2. Add a block using the add_block MATLAB command:
    add_block(simulinkLibraryBlockLocation, blockDestination, 'Position', postion)
  3. Create a subsystem by referencing the blocks you want your subsystem to contain with their handles [which you can get using the MATLAB command get_param(block, ‘handle’)] and by using the following MATLAB command:
    Simulink.BlockDiagram.createSubSystem(subSystemContent);

     

  4. Connect blocks using the add_line MATLAB command:
    add_line(modelName, sourceBlock, destinationBlock, 'autorouting', 'on');
  5. Modify the position using the MATLAB command:
    set_param(block, position, positionValue);
  6. Save and simulate your model using:
    save_system(model) % save the model
    sim(model) % simulate the model

Here’s the complete script:

modelName = 'mySimpleModel';
 
% create and open the test model
open_system(new_system(modelName));
 
offset = 100;
hSubsystems = 60;
 
% position sinewave
xSW= 30;
ySW = 30;
wSW = 30;
hSW = 30;
posSW= [xSW ySW xSW+wSW ySW+hSW];
 
% add the Sine Wave from Simulink library
blockName = 'Sine Wave';
add_block(['simulink/Sources/' blockName], [modelName '/' blockName], 'Position', posSW);
 
% create a subsytem with I/O
add_block('simulink/Sources/In1', [modelName '/In1']);
add_block('simulink/Sinks/Out1', [modelName '/Out1']);
inputPort = get_param([modelName '/In1'], 'handle');
outputPort = get_param([modelName '/Out1'], 'handle');
subSystemContent = [inputPort outputPort];
Simulink.BlockDiagram.createSubSystem(subSystemContent);
 
% delete the I/O created in modelName 
delete_line(modelName,'In1/1','Subsystem/1');
delete_line(modelName,'Subsystem/1','Out1/1');
delete_block([modelName '/In1']);
delete_block([modelName '/Out1']);
 
% add line between the Input and Output ports
add_line(modelName, 'Sine Wave/1', 'Subsystem/1', 'autorouting', 'on');
 
% position Subsystem
wSubsystem = 8;
posSubsystem = posSW; 
posSubsystem(1) = posSubsystem(1)+offset;
posSubsystem(2) = posSW(2)-(hSubsystems-hSW)/2;
posSubsystem(3) = posSubsystem(3)+offset+wSubsystem;
posSubsystem(4) = posSubsystem(2)+hSubsystems;
set_param([modelName '/Subsystem'],'Position', posSubsystem);
 
% position inport
pathInport = [modelName '/Subsystem/In1'];
currentPos = get_param(pathInport, 'Position');
currentHeight = currentPos(4) - currentPos(2);
currentWidth = currentPos(3) - currentPos(1);
posInport(1) = posSW(1);
posInport(2) = posSW(2);
posInport(3) = posInport(1)+currentWidth;
posInport(4) = posInport(2)+currentHeight;
set_param(pathInport,'Position', posInport);
 
% add the Add block from Simulink library
blockName = 'Add';
pathAdd = [modelName '/Subsystem/' blockName];
add_block(['simulink/Math Operations/' blockName], pathAdd);
currentPos = get_param(pathAdd, 'Position');
currentHeight = currentPos(4) - currentPos(2);
currentWidth= currentPos(3) - currentPos(1);
posAdd(1) = posInport(1) + offset;
posAdd(2) = posInport(2)-1;
posAdd(3) = posAdd(1)+currentWidth;
posAdd(4) = posAdd(2)+currentHeight;
set_param(pathAdd,'Position', posAdd);
 
% add line between the Input and Output ports
add_line([modelName '/Subsystem'], 'In1/1', 'Add/1', 'autorouting', 'on');
 
blockName = 'Constant';
pathConstant = [modelName '/Subsystem/' blockName];
add_block(['simulink/Sources/' blockName], pathConstant);
currentPos = get_param(pathConstant, 'Position');
currentHeight = currentPos(4) - currentPos(2);
currentWidth = currentPos(3) - currentPos(1);
posConstant(1) = posSW(1);
posConstant(2) = posSW(2)+offset/2;
posConstant(3) = posConstant(1)+currentWidth;
posConstant(4) = posConstant(2)+currentHeight;
set_param(pathConstant,'Position', posConstant);
 
% add line between the Input and Output ports
add_line([modelName '/Subsystem'], [blockName '/1'], 'Add/2', 'autorouting', 'on');
 
% position outport
blockName = 'Out1';
pathOutport = [modelName '/Subsystem/' blockName];
currentPos = get_param(pathOutport, 'Position');
currentHeight = currentPos(4) - currentPos(2);
currentWidth = currentPos(3) - currentPos(1);
posOutport(1) = posAdd(1) + offset;
posOutport(2) = posInport(2) + (posAdd(4) - posAdd(2))/2 - currentHeight/2;
posOutport(3) = posOutport(1)+currentWidth;
posOutport(4) = posOutport(2)+currentHeight;
set_param(pathOutport,'Position', posOutport);
 
% add line between the Input and Output ports
add_line([modelName '/Subsystem'], 'Add/1', [blockName '/1'], 'autorouting', 'on');
 
 
blockName = 'Scope';
pathScope = [modelName '/' blockName];
add_block(['simulink/Sinks/' blockName], pathScope);
currentPos = get_param(pathScope, 'Position');
currentHeight = currentPos(4) - currentPos(2);
currentWidth = currentPos(3) - currentPos(1);
posScope(1) = posSubsystem(1) + offset;
posScope(2) = posSubsystem(2) + (posSubsystem(4) - posSubsystem(2))/2 - currentHeight/2;
posScope(3) = posScope(1)+currentWidth;
posScope(4) = posScope(2)+currentHeight;
set_param(pathScope,'Position', posScope);
 
% add line between the Input and Output ports
add_line(modelName, 'Subsystem/1', [blockName '/1'], 'autorouting', 'on');
 
save_system(modelName)
sim(modelName)

On the same topic, the following MathWorks documentation can be very helpful: Programmatic Modeling Basics.

Exit mobile version