Main Content

Export Classification Model to MATLAB Coder to Generate C/C++ Code

After you train a model in the Classification Learner app, you can export it to MATLAB® Coder™. Then, you can generate C/C++ code for prediction with new data.

The table below lists the classification models that support code generation.

Model TypeUnderlying Model Object
Decision TreeClassificationTree or CompactClassificationTree
Discriminant AnalysisClassificationDiscriminant or CompactClassificationDiscriminant
Naive Bayes ClassifierClassificationNaiveBayes or CompactClassificationNaiveBayes
Support Vector MachineClassificationSVM (binary), CompactClassificationSVM (binary), ClassificationECOC (multiclass), or CompactClassificationECOC (multiclass)
Efficiently Trained Linear ClassifierClassificationLinear (binary), ClassificationECOC (multiclass), or CompactClassificationECOC (multiclass)
Nearest Neighbor ClassifierClassificationKNN
Kernel ApproximationClassificationKernel (binary), ClassificationECOC (multiclass), or CompactClassificationECOC (multiclass)
Ensemble ClassifierClassificationEnsemble, CompactClassificationEnsemble, or ClassificationBaggedEnsemble
Neural NetworkClassificationNeuralNetwork or CompactClassificationNeuralNetwork

Note

To export a model from Classification Learner to MATLAB Coder:

  1. Train all models that are supported for code generation. On the Learn tab, in the Models section, click All Codegen Supported. In the Train section, click Train All.

  2. Select a trained model in the Models pane. In the Export section, click Export Model and select Export Model to Coder.

  3. In the Export Classification Model to Coder dialog box, edit the names of the entry-point function file, the trained machine learning model file, and the sample predictor data file, if necessary. You can specify the entry-point function to accept a single data set or a list of individual predictors. Enter a name for your coder project, and select a folder for your project files. Ensure that Open MATLAB Coder app after export is selected and click Export.

    Export Classification Model to Coder dialog box with options selected

  4. In the Export Classification Model to Coder dialog box, click OK.

    Export Classification Model to Coder dialog box

    The app automatically sets the following project configuration options in MATLAB Coder:

    • The default coder project build type is MEX file.

    • The entry-point function is prepopulated.

    • The data input types are configured using sample predictor data with variable-size input; that is, a variable number of observations can be passed to generated code (see Specify Variable-Size Arguments for Code Generation).

    The project is prepared for code generation.

  5. In the MATLAB Coder tab of the MATLAB window, click Generate Code and Build. For more information on generating code with MATLAB Coder, see Get Started with MATLAB Coder (MATLAB Coder).

In some cases, you might need to export your trained model to the MATLAB workspace and use the saveLearnerForCoder, loadLearnerForCoder, and codegen (MATLAB Coder) functions to generate code. For example, you might want to add custom instructions in your entry-point function prior to generating code. For detailed examples, see Generate Code at Command Line Using Model Exported from Machine Learning App and Code Generation for Binary GLM Logistic Regression Model Trained in Classification Learner. For more information on Statistics and Machine Learning Toolbox™ functions that support C/C++ code generation, see Code Generation.

See Also

Topics