Model Export to C Code
Export to C Code
In addition to the model-specific *.c files, the following sample files are created, depending on the export type:
Type |
generated sample files |
purpose |
---|---|---|
Model Prediction (Basic) |
mexPredict.c |
used to create a MATLAB mex file |
slPredict.c |
used to create an S function |
MEX file
A MEX file based on an exported model can be generated in MATLAB® as follows:
mex mexPredict.c -DNUMINPUTs=<n_outputs> -DCALLFUNC=predict_<output> -output predict_<output>
<n_outputs> = number of outputs
This generates the file predict_<output>Matlab.mexw64, which can then be called in MATLAB® as follows:
[1.1 2.2 3.3 ...] = n_inputs elements
This results in:
ans = 23.32343
S function
An S function based on an exported model can be generated in MATLAB® as follows:
mex slPredict.c -DNUMINPUTS=<n_inputs> -DCALLFUNC=predict_<output> -output predict_<output>
<n_inputs> = number of inputs
This generates the file predict_<output>Simulink.mexw64.
Format of an exported predict_<output>.c file (3 active inputs):
#include <math.h>
void predict_Fuel_mass(double* pInArray, double* pOutPrediction)
{
[...]
}
The above example can be used as follows:
extern predict_Fuel_mass(double*, double*);
double modelInput[3];
double modelOutput;
modelInput[0] = 1.1;
modelInput[1] = 2.2;
modelInput[2] = 3.3;
predict_Fuel_mass(modelInput, modelOutput);
The modelOutput variable then contains the model prediction for the input vector [1.1, 2.2, 3.3].