Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Save model object in file for code generation

To generate C/C++ code for the object functions
(`predict`

, `random`

,
`knnsearch`

, or `rangesearch`

)
of machine learning models, use `saveCompactModel`

,
`loadCompactModel`

, and
`codegen`

. After training
a machine learning model, save the model by using
`saveCompactModel`

. Define an entry-point
function that loads the model by using `loadCompactModel`

and calls an object function. Then use `codegen`

or the
MATLAB^{®}
Coder™ app to generate C/C++ code. Generating C/C++ code requires
MATLAB
Coder.

This flow chart shows the code generation workflow for the object functions of
machine learning models. Use `saveCompactModel`

for the
highlighted step.

`saveCompactModel(Mdl,filename)`

`saveCompactModel(`

prepares a classification model, regression model, or nearest
neighbor searcher (`Mdl`

,`filename`

)`Mdl`

) for code generation by
reducing its memory footprint and then saving it in the MATLAB formatted binary file (MAT-file) named
`filename`

. You can pass
`filename`

to `loadCompactModel`

to reconstruct the model object from the
`filename`

file.

`saveCompactModel`

reduces the memory
footprint of classification and regression models (`Mdl`

) by removing
properties that are not required for prediction.

For a model that has a corresponding compact model, the

`saveCompactModel`

function applies the appropriate`compact`

function to the model before saving it.For a model that does not have a corresponding compact model, such as

`ClassificationLinear`

,`RegressionLinear`

,`ExhaustiveSearcher`

, and`KDTreeSearcher`

, the`saveCompactModel`

function saves the model as it is.A

`ClassificationKNN`

model object is a full object that does not have a corresponding compact object. For this model,`saveCompactModel`

saves a compact version that does not include the hyperparameter optimization properties.

`loadCompactModel`

loads the reduced version saved by
`saveCompactModel`

.

Use a coder configurer created by

`learnerCoderConfigurer`

for the models listed in this table.Model Coder Configurer Object Support vector machine (SVM) regression `RegressionSVMCoderConfigurer`

SVM for one-class and binary classification `ClassificationSVMCoderConfigurer`

Multiclass model for SVMs `ClassificationECOCCoderConfigurer`

After training a machine learning model, create a coder configurer of the model. Use the object functions and properties of the configurer to configure code generation options and to generate code for the

`predict`

and`update`

functions of the model. If you generate code using a coder configurer, you can update model parameters in the generated code without having to regenerate the code. For details, see Code Generation for Prediction and Update Using Coder Configurer.

- Introduction to Code Generation
- Code Generation for Prediction of Machine Learning Model at Command Line
- Code Generation for Prediction of Machine Learning Model Using MATLAB Coder App
- Code Generation for Nearest Neighbor Searcher
- Specify Variable-Size Arguments for Code Generation
- Code Generation Support, Usage Notes, and Limitations