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


expand all

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

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


expand all

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


expand all

ClassificationECOC PredictClassify observations using error-correcting output codes (ECOC) classification model
ClassificationEnsemble PredictClassify observations using ensemble of decision trees
ClassificationKNN PredictClassify observations using nearest neighbor classification model
ClassificationLinear PredictClassify observations using linear classification model
ClassificationNeuralNetwork PredictClassify observations using neural network classification model
ClassificationSVM PredictClassify observations using support vector machine (SVM) classifier for one-class and binary classification
ClassificationTree PredictClassify observations using decision tree classifier
RegressionEnsemble PredictPredict responses using ensemble of decision trees for regression
RegressionGP PredictPredict responses using Gaussian process (GP) regression model
RegressionLinear PredictPredict responses using linear regression model
RegressionNeuralNetwork PredictPredict responses using neural network regression model
RegressionSVM PredictPredict responses using support vector machine (SVM) regression model
RegressionTree PredictPredict responses using regression tree model


Code Generation Workflows

Classification and Regression Predict Blocks

Code Generation Applications