0
0
MatlabHow-ToBeginner ยท 3 min read

How to Create a Model in Simulink in MATLAB Quickly

To create a model in Simulink in MATLAB, use the new_system function to start a new model and open_system to open it. Then add blocks with add_block and connect them before saving and running the model.
๐Ÿ“

Syntax

Here are the main commands to create and manage a Simulink model in MATLAB:

  • new_system('modelName'): Creates a new empty model with the given name.
  • open_system('modelName'): Opens the model window to view and edit.
  • add_block('sourceBlock', 'destinationBlock'): Adds a block from Simulink library to your model.
  • add_line('modelName', 'srcPort', 'dstPort'): Connects blocks with lines.
  • save_system('modelName'): Saves the model file.
  • sim('modelName'): Runs the simulation.
matlab
new_system('myModel')
open_system('myModel')
add_block('simulink/Sources/Sine Wave', 'myModel/Sine Wave')
add_block('simulink/Sinks/Scope', 'myModel/Scope')
add_line('myModel', 'Sine Wave/1', 'Scope/1')
save_system('myModel')
sim('myModel')
๐Ÿ’ป

Example

This example creates a simple Simulink model with a Sine Wave source connected to a Scope block to visualize the signal.

matlab
new_system('simpleModel')
open_system('simpleModel')
add_block('simulink/Sources/Sine Wave', 'simpleModel/Sine Wave')
add_block('simulink/Sinks/Scope', 'simpleModel/Scope')
add_line('simpleModel', 'Sine Wave/1', 'Scope/1')
save_system('simpleModel')
sim('simpleModel')
Output
Simulating model simpleModel Simulation finished.
โš ๏ธ

Common Pitfalls

Common mistakes when creating Simulink models programmatically include:

  • Not opening the system before adding blocks can cause errors.
  • Incorrect block path names in add_block cause failures.
  • Forgetting to connect blocks with add_line results in disconnected models.
  • Not saving the model before simulation can lose changes.

Always check block library paths and use open_system before editing.

matlab
try
    add_block('simulink/Sources/Sine Wave', 'modelWithoutOpen/Sine Wave')
catch e
    disp('Error: Model not opened before adding blocks.')
end

new_system('modelCorrect')
open_system('modelCorrect')
add_block('simulink/Sources/Sine Wave', 'modelCorrect/Sine Wave')
Output
Error: Model not opened before adding blocks.
๐Ÿ“Š

Quick Reference

Summary of key commands to create and run a Simulink model:

CommandDescription
new_system('name')Create a new empty Simulink model
open_system('name')Open the model window
add_block('source', 'dest')Add a block from library to model
add_line('model', 'srcPort', 'dstPort')Connect blocks with lines
save_system('name')Save the model file
sim('name')Run the simulation
โœ…

Key Takeaways

Use new_system and open_system to create and open a new Simulink model.
Add blocks with add_block and connect them using add_line before saving.
Always save your model before running sim to keep changes.
Check block library paths carefully to avoid errors.
Open the model before adding blocks to prevent common mistakes.