How to Use Model Reference in Simulink: Step-by-Step Guide
In Simulink, use
Model Reference blocks to include one model inside another, enabling modular design and faster simulation. Simply drag a Model Reference block into your main model and select the referenced model file to connect them.Syntax
The Model Reference block syntax involves specifying the referenced model file inside the block parameters. The main components are:
Model Reference Block: The block placed in your main model.Referenced Model: The separate Simulink model file (.slx) you want to include.Input/Output Ports: Connect signals between the main model and the referenced model.
This setup allows the main model to simulate the referenced model as a component.
text
1. Open your main Simulink model. 2. From the Simulink Library Browser, drag the 'Model' block from the 'Ports & Subsystems' library. 3. Double-click the Model block. 4. In the dialog, enter the name of the referenced model file (e.g., 'subsystem_model'). 5. Connect input and output ports as needed.
Example
This example shows how to use a model reference to include a simple subsystem model inside a main model.
Suppose you have a model named subsystem_model.slx that takes an input signal, doubles it, and outputs the result.
Steps:
- Create
subsystem_model.slxwith a Gain block set to 2. - Create a main model.
- Add a
Model Referenceblock and set its model name tosubsystem_model. - Connect a Constant block to the input and a Scope block to the output.
- Run the simulation to see the doubled value.
matlab
% Create subsystem_model.slx open_system(new_system('subsystem_model')); add_block('simulink/Sources/Constant', 'subsystem_model/Constant', 'Value', '3'); add_block('simulink/Math Operations/Gain', 'subsystem_model/Gain', 'Gain', '2'); add_block('simulink/Sinks/Out1', 'subsystem_model/Out1'); add_line('subsystem_model', 'Constant/1', 'Gain/1'); add_line('subsystem_model', 'Gain/1', 'Out1/1'); save_system('subsystem_model'); close_system('subsystem_model'); % Create main model open_system(new_system('main_model')); add_block('simulink/Ports & Subsystems/Model', 'main_model/ModelReference'); set_param('main_model/ModelReference', 'ModelName', 'subsystem_model'); add_block('simulink/Sources/Constant', 'main_model/Constant', 'Value', '5'); add_block('simulink/Sinks/Scope', 'main_model/Scope'); add_line('main_model', 'Constant/1', 'ModelReference/1'); add_line('main_model', 'ModelReference/1', 'Scope/1'); sim('main_model');
Output
Simulation runs showing the Scope block displaying the output value 10 (5 input doubled by subsystem).
Common Pitfalls
- Model name mismatch: The referenced model name in the block must exactly match the referenced model file name without extension.
- Unsaved referenced model: Always save the referenced model before using it in a Model Reference block.
- Port mismatch: Ensure the input and output ports in the referenced model match the connections in the main model.
- Simulation settings: The referenced model must be compatible with the main model's solver and configuration settings.
matlab
%% Wrong way: Referencing an unsaved model % set_param('main_model/ModelReference', 'ModelName', 'unsaved_model') % This causes an error because 'unsaved_model.slx' does not exist. %% Right way: Save the referenced model first % save_system('referenced_model') % set_param('main_model/ModelReference', 'ModelName', 'referenced_model')
Quick Reference
Model Reference Block Cheat Sheet:
| Step | Action |
|---|---|
| 1 | Drag Model Reference block into main model |
| 2 | Set referenced model name in block parameters |
| 3 | Connect input/output ports properly |
| 4 | Save all models before simulation |
| 5 | Run simulation and verify outputs |
| Step | Action |
|---|---|
| 1 | Drag Model Reference block into main model |
| 2 | Set referenced model name in block parameters |
| 3 | Connect input/output ports properly |
| 4 | Save all models before simulation |
| 5 | Run simulation and verify outputs |
Key Takeaways
Use Model Reference blocks to include one Simulink model inside another for modular design.
Always save the referenced model before linking it in the main model.
Ensure input and output ports match between the main and referenced models.
Set the referenced model name exactly in the Model Reference block parameters.
Check solver and configuration compatibility between models for smooth simulation.