Fit a linear regression model, and then save the model by using `saveCompactModel`

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

and calls the `predict`

function of the fitted model. Then use `codegen`

to generate C/C++ code. Note that generating C/C++ code requires MATLAB® Coder™.

This example briefly explains the code generation workflow for the prediction of linear regression models at the command line. For more details, see Code Generation for Prediction of Machine Learning Model at Command Line. You can also generate code using the MATLAB Coder app. For details, see Code Generation for Prediction of Machine Learning Model Using MATLAB Coder App.

**Train Model**

Load the `carsmall`

data set, and then fit the quadratic regression model.

**Save Model**

Save the fitted quadratic model to the file `QLMMdl.mat`

by using `saveCompactModel`

.

**Define Entry-Point Function**

Define an entry-point function named `mypredictQLM`

that does the following:

Accept measurements corresponding to X and optional, valid name-value pair arguments.

Load the fitted quadratic model in `QLMMdl.mat`

.

Return predictions and confidence interval bounds.

function [yhat,ci] = mypredictQLM(x,varargin) %#codegen
%MYPREDICTQLM Predict response using linear model
% MYPREDICTQLM predicts responses for the n observations in the n-by-1
% vector x using the linear model stored in the MAT-file QLMMdl.mat, and
% then returns the predictions in the n-by-1 vector yhat. MYPREDICTQLM
% also returns confidence interval bounds for the predictions in the
% n-by-2 vector ci.
CompactMdl = loadCompactModel('QLMMdl');
[yhat,ci] = predict(CompactMdl,x,varargin{:});
end

Add the `%#codegen`

compiler directive (or pragma) to the entry-point function after the function signature to indicate that you intend to generate code for the MATLAB algorithm. Adding this directive instructs the MATLAB Code Analyzer to help you diagnose and fix violations that would result in errors during code generation.

**Note:** If you click the button located in the upper-right section of this example and open the example in MATLAB®, then MATLAB® opens the example folder. This folder includes the entry-point function file.

**Generate Code**

Generate code for the entry-point function using `codegen`

. Because C and C++ are statically typed languages, you must determine the properties of all variables in the entry-point function at compile time. To specify the data type and exact input array size, pass a MATLAB® expression that represents the set of values with a certain data type and array size. Use `coder.Constant`

for the names of name-value pair arguments.

If the number of observations is unknown at compile time, you can also specify the input as variable-size by using `coder.typeof`

. For details, see Specify Variable-Size Arguments for Code Generation and Specify Properties of Entry-Point Function Inputs (MATLAB Coder).

`codegen`

generates the MEX function `mypredictQLM_mex`

with a platform-dependent extension.

**Verify Generated Code**

Compare predictions and confidence intervals using `predict`

and `mypredictQLM_mex`

. Specify name-value pair arguments in the same order as in the `-args`

argument in the call to `codegen`

.

The returned values from `mypredictQLM_mex`

might include round-off differences compared to the values from `predict`

. In this case, compare the values allowing a small tolerance.

ans =
0x1 empty double column vector

ans =
0x1 empty double column vector

The comparison confirms that the returned values are equal within the tolerance `1e–6`

.

Plot the returned values for comparison.