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

Model Prediction and Gradient

mexPredictGrad.c

used to create a MATLAB mex file

MEX file

A MEX file based on an exported ASCMO-STATIC model can be generated in MATLAB® as follows:

Copy
Generate a MEX file
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:

Copy
Call a MEX file
>> predict_<output>([1.1 2.2 3.3 ...])

[1.1 2.2 3.3 ...] = n_inputs elements

This results in:

ans = 23.32343

S function

An S function based on an exported ASCMO-STATIC model can be generated in MATLAB® as follows:

Copy
Generate an S function
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.

EXAMPLE

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