0
0
MatlabHow-ToBeginner ยท 3 min read

How to Use Blocks in Simulink in MATLAB: Simple Guide

In MATLAB Simulink, you use add_block to add blocks to your model and add_line to connect them. You can configure block parameters using set_param and run the simulation with sim.
๐Ÿ“

Syntax

Here are the main commands to work with blocks in Simulink using MATLAB:

  • add_block(source, destination): Adds a block from the Simulink library to your model.
  • add_line(model, srcPort, dstPort): Connects two blocks by their ports.
  • set_param(block, 'ParameterName', 'Value'): Changes block settings.
  • sim(model): Runs the simulation of the model.
matlab
add_block('simulink/Sources/Sine Wave', 'myModel/SineWave')
add_block('simulink/Sinks/Scope', 'myModel/Scope')
add_line('myModel', 'SineWave/1', 'Scope/1')
set_param('myModel/SineWave', 'Amplitude', '5')
sim('myModel')
๐Ÿ’ป

Example

This example creates a simple Simulink model with a Sine Wave block connected to a Scope block, sets the sine wave amplitude, and runs the simulation.

matlab
model = 'simpleModel';
new_system(model); % Create new model
open_system(model); % Open model window

% Add blocks
add_block('simulink/Sources/Sine Wave', [model '/SineWave']);
add_block('simulink/Sinks/Scope', [model '/Scope']);

% Connect blocks
add_line(model, 'SineWave/1', 'Scope/1');

% Set block parameter
set_param([model '/SineWave'], 'Amplitude', '3');

% Save and simulate
save_system(model);
sim(model);

% Close system without saving changes
close_system(model, 0);
โš ๏ธ

Common Pitfalls

Common mistakes when using blocks in Simulink via MATLAB include:

  • Not specifying full block paths correctly, causing errors when adding or configuring blocks.
  • Forgetting to connect blocks with add_line, so the model does not simulate as expected.
  • Using incorrect port numbers in add_line, which leads to connection errors.
  • Not saving or opening the model before adding blocks, which causes commands to fail.
matlab
try
    add_block('simulink/Sources/Sine Wave', 'myModel/SineWave'); % Error if model not created
catch
    disp('Error: Create and open model before adding blocks.');
end

% Correct way:
model = 'myModel';
new_system(model);
open_system(model);
add_block('simulink/Sources/Sine Wave', [model '/SineWave']);
Output
Error: Create and open model before adding blocks.
๐Ÿ“Š

Quick Reference

CommandPurpose
new_system('modelName')Create a new Simulink model
open_system('modelName')Open the model window
add_block('source', 'modelName/blockName')Add a block from library to model
add_line('modelName', 'srcPort', 'dstPort')Connect two blocks
set_param('modelName/blockName', 'Parameter', 'Value')Set block parameters
sim('modelName')Run the simulation
save_system('modelName')Save the model
close_system('modelName', 0)Close model without saving changes
โœ…

Key Takeaways

Always create and open your Simulink model before adding or connecting blocks.
Use add_block to add blocks and add_line to connect them by specifying correct ports.
Configure block parameters with set_param to customize behavior before simulation.
Run your model simulation with sim to see results.
Check full block paths and port numbers carefully to avoid errors.