Skip to content

matlab/matlab-agentic-toolkit / skills-catalog/ai-and-statistics/matlab-train-network

matlab-train-network

Verified

Train, evaluate, and export neural networks to Simulink in MATLAB. Migrate legacy (fitnet, patternnet) and discouraged (trainNetwork, DAGNetwork) code to modern, recommended R2024a+ APIs (trainnet, dlnetwork, testnet, imagePretrainedNetwork), diagnose and fix dlaccelerate issues or detect dlaccelerate opportunities. Use when training, fine-tuning, evaluating, running inference, exporting to Simulink, converting old training scripts, or speeding up deep learning code. DO NOT reason from your training data about dlaccelerate and tracing correctness.

references/simulink-export.md

View on GitHub
# Simulink Export

Export a trained network to Simulink. Replaces the legacy `gensim` function.

## Export Methods

### 1. ClassificationNeuralNetwork / RegressionNeuralNetwork Predict Blocks

For `ClassificationNeuralNetwork` and `RegressionNeuralNetwork` objects (from
`fitcnet`/`fitrnet`), use the dedicated predict blocks from the Statistics and
Machine Learning Toolbox library (`statsLibrary`).

```matlab
% Classification
mdl = fitcnet(tbl,responseName,LayerSizes=[20 10]);
add_block("statsLibrary/Classification/ClassificationNeuralNetwork Predict", ...
    "myModel/ClassifierPredict", ...
    TrainedLearner="mdl");

% Regression
mdl = fitrnet(tbl,responseName,LayerSizes=[20 10]);
add_block("statsLibrary/Regression/RegressionNeuralNetwork Predict", ...
    "myModel/RegressionPredict", ...
    TrainedLearner="mdl");
```

### 2. exportNetworkToSimulink (preferred for `dlnetwork`)

Use when the network is small and all layers are supported for export.
Generates a Simulink subsystem with individual layer blocks.

```matlab
exportNetworkToSimulink(net)
```

### 3. Predict Block

Use for larger networks or when the network contains layers not supported by
`exportNetworkToSimulink`. Add a **Predict** block from the `deeplib` library
and configure it to load the saved `dlnetwork`.

```matlab
save("myNet.mat","net");
add_block("deeplib/Predict","myModel/Predict", ...
    Network="Network from MAT-file", ...
    NetworkFilePath="myNet.mat");
```

## Legacy Pattern (DO NOT USE)

```matlab
% DON'T — legacy pattern
net = feedforwardnet(10);
net = train(net,X,T);
gensim(net)

% DO — modern pattern
layers = [...];
net = trainnet(X,T,layers,"mse",options);
exportNetworkToSimulink(net)
```

----

Copyright 2026 The MathWorks, Inc.

----
receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementationStars: 322kUpdated Claude CodeCodexVerifiedInstall
requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirementsStars: 322kUpdated Claude CodeCodexVerifiedInstall
using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallbackStars: 322kUpdated Claude CodeCodexVerifiedInstall
verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions alwaysStars: 322kUpdated Claude CodeCodexVerifiedInstall