0
0
SimulinkHow-ToBeginner · 4 min read

How to Use Deep Learning in Simulink: Step-by-Step Guide

To use deep learning in Simulink, import a trained deep learning model (like a SeriesNetwork or dlnetwork) into Simulink using the Deep Learning Toolbox blocks. Then, connect the model block to your system inputs and outputs to run inference during simulation or code generation.
📐

Syntax

In Simulink, you use the Deep Learning Toolbox blocks to run deep learning models. The main block is the Predict block that accepts a trained network object.

Typical usage involves:

  • Importing a trained network object (e.g., net = trainedNetwork)
  • Adding the Predict block to your Simulink model
  • Setting the block's parameters to use your network
  • Connecting input signals (images, features) and output signals (predictions)
matlab
net = alexnet; % Load pretrained network

% In Simulink, add 'Predict' block from Deep Learning Toolbox
% Set 'Network' parameter to 'net'
% Connect input image signal to block input
% Connect block output to further processing or display
💻

Example

This example shows how to run a pretrained deep learning model (AlexNet) in Simulink to classify images during simulation.

First, load the pretrained network in MATLAB. Then, open Simulink and add the Predict block. Set the block's network parameter to the loaded network. Connect an image input source (like an Image From File block) to the Predict block. The output will be the predicted class scores.

matlab
net = alexnet; % Load pretrained AlexNet

% Open Simulink model
simulink

% In Simulink:
% 1. Add 'Image From File' block and select an image
% 2. Add 'Predict' block from Deep Learning Toolbox
% 3. Set 'Network' parameter of Predict block to 'net'
% 4. Connect 'Image From File' output to 'Predict' input
% 5. Add 'Display' block to see prediction scores

% Run simulation to see classification results
Output
Simulation runs and outputs classification scores for the input image in the Display block.
⚠️

Common Pitfalls

  • Incorrect input size: Deep learning models expect inputs of specific size and format. Make sure your input images or data match the network input size.
  • Unsupported layers: Some custom or unsupported layers in your trained network may not run in Simulink. Use supported layers or convert custom layers properly.
  • Data type mismatch: Inputs must be of the correct data type (usually single or uint8 for images). Convert data before feeding into the network.
  • Missing Deep Learning Toolbox: Ensure the Deep Learning Toolbox and Simulink support packages are installed.
matlab
%% Wrong input size example
inputImage = rand(100,100,3,'single'); % Wrong size for AlexNet (227x227x3)

% Correct input size
inputImageCorrect = imresize(inputImage,[227 227]);
📊

Quick Reference

StepActionNotes
1Train or load a deep learning model in MATLABUse SeriesNetwork or dlnetwork objects
2Open Simulink and add 'Predict' blockFound in Deep Learning Toolbox library
3Set the network parameter to your trained modelUse variable name of the network
4Connect input data signals matching network input sizeResize and typecast inputs as needed
5Run simulation to get predictionsOutput can be used for control or analysis

Key Takeaways

Import trained deep learning models into Simulink using the Deep Learning Toolbox blocks.
Ensure input data matches the network's expected size and data type.
Use the 'Predict' block to run inference during simulation.
Check for unsupported layers or missing toolboxes to avoid errors.
Simulink enables real-time testing and deployment of deep learning models.