0
0
SimulinkHow-ToBeginner · 4 min read

How to Import Trained Neural Network to Simulink Easily

To import a trained neural network into Simulink, first save your network in MATLAB workspace, then use the Neural Network block from the Deep Learning Toolbox in Simulink. Connect this block to your model and configure it to use the saved network for simulation or code generation.
📐

Syntax

Use the Neural Network block in Simulink to import your trained network. The basic steps are:

  • Save your trained network as a variable in MATLAB workspace, e.g., net.
  • Open Simulink and add the Neural Network block from the Deep Learning Toolbox library.
  • Double-click the block and set the Network parameter to the variable name of your trained network.
  • Connect inputs and outputs as needed in your Simulink model.
matlab
net = trainNetwork(XTrain,YTrain,layers,options);
% Save the trained network as 'net'

% In Simulink:
% 1. Add 'Neural Network' block from Deep Learning Toolbox
% 2. Set 'Network' parameter to 'net'
% 3. Connect inputs and outputs
💻

Example

This example shows how to train a simple neural network in MATLAB and import it into Simulink for simulation.

matlab
% Train a simple network
XTrain = rand(100,10); % 100 samples, 10 features
YTrain = rand(100,1);  % 100 samples, 1 output
layers = [
    featureInputLayer(10)
    fullyConnectedLayer(5)
    reluLayer
    fullyConnectedLayer(1)
    regressionLayer];
options = trainingOptions('sgdm', 'MaxEpochs', 5, 'Verbose', false);
net = trainNetwork(XTrain, YTrain, layers, options);

% Save network to workspace
save('trainedNet.mat', 'net');

% In Simulink:
% 1. Open Simulink and create a new model
% 2. Add 'Neural Network' block from Deep Learning Toolbox
% 3. Set the 'Network' parameter to 'net'
% 4. Connect input source and output sink blocks
% 5. Run the simulation
Output
Training on single GPU. Epoch 1/5 Epoch 2/5 Epoch 3/5 Epoch 4/5 Epoch 5/5 Training finished.
⚠️

Common Pitfalls

  • Network variable not in workspace: The Neural Network block requires the trained network variable to be loaded in MATLAB workspace before simulation.
  • Input size mismatch: Ensure the input data size in Simulink matches the network input size.
  • Unsupported layers: Some custom or unsupported layers may not work in Simulink; use supported layers only.
  • Code generation issues: For code generation, confirm the network supports code generation and configure the block accordingly.
matlab
% Wrong: Network variable not loaded
% In Simulink, setting 'Network' parameter to 'net' without loading it

% Right: Load network before simulation
load('trainedNet.mat');
% Now run Simulink model
📊

Quick Reference

Summary tips for importing trained neural networks to Simulink:

  • Always save and load your trained network variable in MATLAB workspace.
  • Use the Neural Network block from Deep Learning Toolbox.
  • Match input sizes between Simulink and your network.
  • Check layer compatibility for simulation and code generation.
  • Test your model with sample inputs before full deployment.

Key Takeaways

Save your trained neural network as a variable in MATLAB workspace before importing to Simulink.
Use the Neural Network block from Deep Learning Toolbox to load and simulate the network in Simulink.
Ensure input sizes and layer types are compatible between your network and Simulink model.
Load the network variable in MATLAB workspace before running the Simulink simulation.
Check for code generation support if you plan to deploy the model outside Simulink.