0
0
SimulinkHow-ToBeginner · 4 min read

How to Create Custom Library in Simulink: Step-by-Step Guide

To create a custom library in Simulink, start by opening a new model and adding the blocks you want to reuse. Then, save this model as a .slx file and convert it into a library by selecting File > Save As > Library. You can then use this library in other models by dragging blocks from it.
📐

Syntax

Creating a custom library in Simulink involves these main steps:

  • Create a new model: This will hold your reusable blocks.
  • Add blocks: Insert the blocks you want to reuse.
  • Save as library: Use File > Save As > Library to save the model as a library file (.slx).
  • Use library blocks: Open the library in other models and drag blocks to reuse.
matlab
%% No direct code syntax, but here is the MATLAB command to open a new model
new_system('MyLibrary')
open_system('MyLibrary')

% Add blocks programmatically (example: add Gain block)
add_block('simulink/Commonly Used Blocks/Gain','MyLibrary/Gain')

% Convert to library by setting model parameter
set_param('MyLibrary','BlockDiagramType','library')

% Save as library
save_system('MyLibrary','MyLibrary.slx','ExportToVersion','R2024a')

% Close system
close_system('MyLibrary',true)
💻

Example

This example shows how to create a simple custom library with a Gain block and use it in another model.

matlab
%% Step 1: Create library model
new_system('CustomLib')
open_system('CustomLib')
add_block('simulink/Commonly Used Blocks/Gain','CustomLib/CustomGain')
set_param('CustomLib/CustomGain','Gain','5')
set_param('CustomLib','BlockDiagramType','library')
save_system('CustomLib','CustomLib.slx')
close_system('CustomLib')

%% Step 2: Use library in new model
new_system('TestModel')
open_system('TestModel')
add_block('CustomLib/CustomGain','TestModel/UsedGain')
open_system('TestModel')
Output
A new model named 'TestModel' opens with a Gain block named 'UsedGain' linked to the custom library 'CustomLib'.
⚠️

Common Pitfalls

  • Not setting model as library: Forgetting to set BlockDiagramType to library means the model won't behave as a library.
  • Modifying library blocks directly: Changes to library blocks affect all models using them; use Link Options to break links if needed.
  • File path issues: Ensure the library file is in MATLAB path or specify full path when adding blocks.
matlab
%% Wrong way: Not setting as library
new_system('WrongLib')
add_block('simulink/Commonly Used Blocks/Gain','WrongLib/Gain')
save_system('WrongLib','WrongLib.slx')
% This model is not a library and blocks cannot be reused as library blocks

%% Right way: Set as library
set_param('WrongLib','BlockDiagramType','library')
save_system('WrongLib','WrongLib.slx')
📊

Quick Reference

Summary tips for creating and using custom Simulink libraries:

  • Create a new model and add reusable blocks.
  • Set model parameter BlockDiagramType to library.
  • Save the model as a .slx library file.
  • Open the library in other models to drag and drop blocks.
  • Use Link Options to control block link behavior.
  • Keep library files in MATLAB path for easy access.

Key Takeaways

Set the model's BlockDiagramType to 'library' to create a proper Simulink library.
Save your reusable blocks in a library file (.slx) for easy sharing across models.
Always keep your library file in MATLAB path or specify full path when using blocks.
Use Link Options to manage how changes to library blocks affect linked blocks.
Test your library by adding blocks to a new model to ensure proper reuse.