Calling a machine learning model on simulink
14 views (last 30 days)
Show older comments
Hello everyone, I'm using Simulink for running simulations, and I'd like to call a trained machine learning model (in my case, a TreeBagger) in MATLAB to make predictions in Simulink. However, I'm having some trouble with it. Has anyone already worked with this?
0 Comments
Answers (2)
Yash
on 2 Dec 2023
Hi Meriem,
I understand that you want to call a machine learning model in Simulink that you trained in MATLAB. To do this you can export your trained model to a MATLAB function and then call that function in Simulink using a MATLAB Function block. To export your trained model to a MATLAB function you can use the codegen function. This function generates C/C++ code from MATLAB code, including trained models. Here's an example code snippet:
>> net = trainNetwork(XTrain,YTrain,layers,options); % trains a neural network
>> codegen net -args {XTest} -config:dll % generates a DLL file containing the network
This code trains a neural network using trainNetwork, and then generates a DLL file containing the network using codegen. The -args option specifies the input arguments for the generated function, and the -config:dll option specifies that the output should be a DLL file.
Please note that here are some limitations to what types of models can be exported using codegen. Not all MATLAB functions and toolboxes are supported, and some models may require additional configuration or customization to be exported successfully. Additionally, some models may not be suitable for deployment on certain hardware platforms. To determine whether a specific model can be exported using codegen, you can consult the documentation for the function or toolbox that was used to create the model here: https://in.mathworks.com/help/coder/ref/codegen.html
To implement MATLAB functions in Simulink, you can use the MATLAB Function Block. To know how to use it, you can read its documentation here: https://in.mathworks.com/help/simulink/ug/what-is-a-matlab-function-block.html
Hope this helps!
Hamid Satarboroujeni
on 14 Dec 2023
Hi Meriem,
If you have a trained machine learning model in MATLAB and want to use it for predictions in Simulink, you can use one of the Predict blocks in the Statistics and Machine Learning Toolbox. You can see a list of all native Simulink blocks for prediction here:
In the case of a tree bagger model, you can use the ClassificationEnsemble Predict or the RegressionEnsemble Predict blocks:
For these blocks, you do not need to use the saveLearnerForCoder function because the blocks can work with the trained model directly and do not need any conversions.
There is an "older" approach using a user-created MATLAB Function block as pointed out above. Compared to the native Simulink blocks, the MATLAB Function block needs to be created manually and has certain limitations, such as variable-size signals and data type support. The blocks support all data types, including single precision and fixed-point.
The answer by Yash above is for deep learning models and cannot be used for a TreeBagger model, which is under the Statistics and Machine Learning Toolbox.
I hope this answers the question.
0 Comments
See Also
Categories
Find more on Code Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!