Export Regression Model to Make Predictions in Simulink
After you train a model in Regression Learner, you can export the model and training data to Simulink®. You can then make predictions with the model in Simulink using the exported training data or new data.
You can export all models for these model types:
Regression Tree
Support Vector Machine
Efficiently Trained Linear Regression
Gaussian Process Regression
Kernel Approximation Regression
Ensemble
Neural Network
You cannot export any Linear Regression models to Simulink.
Note
You cannot export a model that contains categorical predictors to Simulink. If
your input data includes categorical data, you must either exclude the categorical
predictors using Feature Selection (see Feature Selection and Feature Transformation Using Regression Learner App), or prepare an encoded version of the
categorical data before you import it into the app. At the MATLAB command line, use
dummyvar
to convert each
categorical variable to a numeric matrix of dummy variables. Then, concatenate all
dummy variable matrices and any other numeric predictors. For more details, see
Dummy Variables.
To export a model to Simulink:
Train a supported model in Regression Learner using a data set that does not contain categorical predictors. To train all supported models, click All Simulink Supported in the Models section of the Learn tab and then click Train All. Select the model you want to export in the Models pane.
In the Export section of the Learn tab, click Export Model and select Export Model to Simulink.
In the Export Regression Model to Simulink dialog box, click the option to save the model and input data to the MATLAB workspace. You can also save the model and input data to the Simulink model workspace. Edit the name of the exported regression model variable, and the name of the exported Simulink model input data variable if necessary. To avoid overwriting previously exported variables, the app increments the default name of the model
trainedModel
and the model inputinputData
every time you export (for example,trainedModel1
). Click Export.In the Save Simulink Model As dialog box, edit the
.slx
file name, if necessary, and then click Save. If you specify to save to the MATLAB workspace, the software creates a.mat
file containing the trained model and input data in the same directory.The new variables (for example,
trainedModel
andinputData
) appear in the MATLAB workspace. The app launches Simulink and creates a new Simulink model. The software runs the code in thePreLoadFcn
callback function of the Simulink model to load the data ininputData
, and then loads the Simulink model. For more information about callback functions in Simulink, see Model Callbacks (Simulink).The From Workspace (Simulink) block is connected to a Predict block of the type corresponding to your exported regression model. For example, if you exported a regression tree model, the block is a RegressionTree Predict block. You can double-click the Predict block to specify data types and additional options. For more information, see the documentation page of the corresponding Predict block.
If you trained the model in Regression Learner using feature selection or with PCA enabled, the From Workspace block is connected to a Prediction subsystem block labeled with the model type. You can double-click the Prediction subsystem block to access the Predict block, the
Feature Selection
subsystem block, and theDimensionality Reduction (PCA)
subsystem block.The model is configured to make predictions using the observations in
inputData
. To make predictions using new data, double-click the From Workspace block, and enter the name of a MATLAB variable or expression that evaluates to data (for more information, see Load Data Using the From Workspace Block (Simulink)).To run the Simulink model, click Run on the Simulation tab. You can use the Simulation Data Inspector (Simulink) to view the logged data of a To Workspace (Simulink) block.
The To Workspace block outputs the predicted responses from the Predict block to a new variable named
outputPredictions
in the MATLAB workspace. You can double-click the To Workspace block to edit the name of the workspace variable, if necessary, and specify other options, such as the workspace variable format, number of samples to log, and effective sample rate (decimation factor) for the logged data.
For an example showing how to use Statistics and Machine Learning Toolbox™ blocks for prediction in Simulink, see Predict Responses Using RegressionTree Predict Block.
See Also
Topics
- Select Data for Regression or Open Saved App Session
- Train Regression Models in Regression Learner App
- Generate MATLAB Code to Train Model with New Data
- Export Regression Model to Predict New Data
- Export Regression Model to MATLAB Coder to Generate C/C++ Code
- Export Regression Model for Deployment to MATLAB Production Server