Main Content

Code Generation

Generate C/C++ code and MEX functions for Statistics and Machine Learning Toolbox™ functions

MATLAB® Coder™ generates readable and portable C and C++ code from Statistics and Machine Learning Toolbox functions that support code generation. For example, you can classify new observations on hardware devices that cannot run MATLAB by deploying a trained support vector machine (SVM) classification model to the device using code generation.

You can generate C/C++ code for these functions in several ways:

  • Use saveLearnerForCoder, loadLearnerForCoder, and codegen (MATLAB Coder) for an object function of a machine learning model.

  • Use a coder configurer created by learnerCoderConfigurer for predict and update object functions of a machine learning model. Configure code generation options by using the configurer and update model parameters in the generated code.

  • Use codegen for other functions that support code generation.

You can also generate fixed-point C/C++ code for the prediction of some machine learning models. This type of code generation requires Fixed-Point Designer™.

To integrate the prediction of a machine learning model into Simulink®, use a MATLAB Function block or the Simulink blocks in the Statistics and Machine Learning Toolbox library.

To learn about code generation, see Introduction to Code Generation.

For a list of functions that support code generation, see Function List (C/C++ Code Generation).

Functions

expand all

saveLearnerForCoderSave model object in file for code generation (Since R2019b)
loadLearnerForCoderReconstruct model object from saved model for code generation (Since R2019b)
generateLearnerDataTypeFcnGenerate function that defines data types for fixed-point code generation (Since R2019b)

Create Coder Configurer Object

learnerCoderConfigurerCreate coder configurer of machine learning model

Work with Coder Configurer Object

generateCodeGenerate C/C++ code using coder configurer
generateFilesGenerate MATLAB files for code generation using coder configurer
validatedUpdateInputsValidate and extract machine learning model parameters to update
updateUpdate model parameters for code generation

Objects

expand all

ClassificationTreeCoderConfigurerCoder configurer of binary decision tree model for multiclass classification (Since R2019b)
ClassificationSVMCoderConfigurerCoder configurer for support vector machine (SVM) for one-class and binary classification
ClassificationLinearCoderConfigurerCoder configurer for linear binary classification of high-dimensional data (Since R2019b)
ClassificationECOCCoderConfigurerCoder configurer for multiclass model using binary learners
RegressionTreeCoderConfigurerCoder configurer of binary decision tree model for regression (Since R2019b)
RegressionSVMCoderConfigurerCoder configurer for support vector machine (SVM) regression model
RegressionLinearCoderConfigurerCoder configurer for linear regression model with high-dimensional data (Since R2019b)

Blocks

expand all

ClassificationECOC PredictClassify observations using error-correcting output codes (ECOC) classification model (Since R2023a)
ClassificationEnsemble PredictClassify observations using ensemble of decision trees (Since R2021a)
ClassificationKNN PredictClassify observations using nearest neighbor classification model (Since R2022b)
ClassificationLinear PredictClassify observations using linear classification model (Since R2023a)
ClassificationNaiveBayes PredictClassify observations using naive Bayes model (Since R2023b)
ClassificationNeuralNetwork PredictClassify observations using neural network classification model (Since R2021b)
ClassificationSVM PredictClassify observations using support vector machine (SVM) classifier for one-class and binary classification (Since R2020b)
ClassificationTree PredictClassify observations using decision tree classifier (Since R2021a)
ClassificationDiscriminant PredictClassify observations using discriminant analysis model (Since R2024a)
RegressionEnsemble PredictPredict responses using ensemble of decision trees for regression (Since R2021a)
RegressionGP PredictPredict responses using Gaussian process (GP) regression model (Since R2022a)
RegressionLinear PredictPredict responses using linear regression model (Since R2023a)
RegressionNeuralNetwork PredictPredict responses using neural network regression model (Since R2021b)
RegressionSVM PredictPredict responses using support vector machine (SVM) regression model (Since R2020b)
RegressionTree PredictPredict responses using regression tree model (Since R2021a)
IncrementalClassificationLinear PredictClassify observations using incremental linear classification model (Since R2023b)
IncrementalClassificationLinear FitFit incremental linear binary classification model (Since R2023b)
IncrementalRegressionLinear PredictPredict responses using incremental linear regression model (Since R2023b)
IncrementalRegressionLinear FitFit incremental linear regression model (Since R2023b)
IncrementalClassificationECOC FitFit incremental ECOC classification model (Since R2024a)
IncrementalClassificationECOC PredictClassify observations using incremental ECOC classification model (Since R2024a)
Update MetricsUpdate performance metrics in incremental learning model given new data (Since R2023b)
KNN SearchFind k-nearest neighbors using searcher object (Since R2023b)

Topics

Code Generation Workflows

Classification and Regression Predict Blocks

Incremental Learning Blocks

Cluster Analysis Blocks

Code Generation Applications