Main Content

Export Classification Model to Make Predictions in Simulink

After you train a model in Classification 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:

  • Discriminant Analysis

  • Naive Bayes Classifier

  • Support Vector Machine

  • Efficiently Trained Linear Classifier

  • Nearest Neighbor Classifier

For these model types, you can export the models specified:

  • Decision Tree — All models trained without surrogate decision splits

  • Logistic Regression Classifier — All models except Binary GLM Logistic Regression

  • Ensemble Classifier — All models except Subspace Discriminant and Subspace KNN

  • Kernel Approximation — All models except those trained on multiclass data

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 Classification 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:

  1. Train a supported model in Classification 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.

  2. In the Export section of the Learn tab, click Export Model and select Export Model to Simulink.

  3. In the Export Classification 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 classification 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 input inputData every time you export (for example, trainedModel1). Select the option to show an output port for the predicted class scores in the Simulink model. Click Export.

    Export Classification Model to Simulink dialog box with options selected

  4. 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 and inputData) appear in the MATLAB workspace. The app launches Simulink and creates a new Simulink model. The software runs the code in the PreLoadFcn callback function of the Simulink model to load the data in inputData, and then loads the Simulink model. For more information about callback functions in Simulink, see Model Callbacks (Simulink).

    Exported Classification Leaner app model in Simulink

    The From Workspace (Simulink) block is connected to a Predict block of the type corresponding to your exported classification model. For example, if you exported a Gaussian Naive Bayes model, the block is a ClassificationNaiveBayes Predict block. When you export some models, such as Efficient Logistic Regression and Linear SVM, and your data has three or more classes, the block is a ClassificationECOC 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 Classification 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 the Dimensionality Reduction (PCA) subsystem block.

  5. 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)).

  6. 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 labels 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.

    If you specified to show an output port for the predicted class scores in the Simulink model, an additional block labeled To Workspace1 is connected to the score output of the Predict block. The To Workspace1 block outputs the classification scores to a new variable named outputScores in the MATLAB workspace. outputScores is an n-by-c numeric array, where n is the number of observations in inputData, and c is the number of classes in trainedModel. You can double-click the To Workspace1 block to edit the name of the workspace variable and specify other options.

For an example showing how to use Statistics and Machine Learning Toolbox™ blocks for prediction in Simulink, see Predict Class Labels Using ClassificationNaiveBayes Predict Block.

See Also

Topics