Main Content

Configure Model to Generate C API Code for Model Parameters

This example shows how to configure the model to include C API for model parameters in the generated code.

Open the model CapiConfigDemo.

capiMdl = "CapiConfigDemo";
open_system(capiMdl);

Enable C API Generation for Parameters

To enable C API code generation for parameters:

  1. Open the Configuration Parameters dialog box.

  2. Navigate to the Code Generation > Interface pane.

  3. In the Data exchange interface > Generate C API for section, select parameters.

  4. Click OK.

Configuration Parameters dialog box. The Code Generation > Interface pane is open. In the Generate C API for section, the parameters box is selected.

Alternatively, enter this in the Command Window:

set_param(capiMdl,RTWCAPIParams=true)

Define Variables and Use Them as Model Parameters

In the context of the C API, model parameters are any variables that are used by the model. The variables can be MATLAB® variables or Simulink.Parameter variables, and they can be defined in the base workspace or the model workspace.

Get handles to the to Unit Delay blocks. Then get the model workspace object.

unitDelayHandle_1 = get_param(capiMdl+"/unitDelay1","Handle");
unitDelayHandle_2 = get_param(capiMdl+"/unitDelay2","Handle");
modelWorkSpace = get_param(capiMdl,"ModelWorkspace");

Define two Simulink.Parameter variables, var_1 in the base workspace and var_2 in the model workspace.

var_1 = Simulink.Parameter(21.04);
assignin(modelWorkSpace,var_2=Simulink.Parameter(2005));

To designate these two variables as model parameters, your model needs to use them. Use var_1 and var_2 as the initial condition of your Unit Delay blocks. To do this, specify the initial condition of unitDelay1 as var_1 and specify the initial condition of unitDelay2 as var_2.

set_param(unitDelayHandle_1,InitialCondition="var_1");
set_param(unitDelayHandle_2,InitialCondition="var_2");

Specify Parameter Storage Classes

One way to prevent the code generator from optimizing parameters away is to specify their storage class as different from "Auto". Specify the storage class of var_1 as "Model default" and specify the storage class of var_2 as "Auto".

var_1.CoderInfo.StorageClass = "Model default";

To specify the storage class of var_2, you need to use the getVariable command with the model space object.

modelSpaceVar_2 = getVariable(modelWorkSpace,"var_2");
modelSpaceVar_2.CoderInfo.StorageClass = "Auto";

You expect the C API to be generated for var_1, but not for var_2.

Generate Model Code

Use the slbuild command to generate code from your model. Use evalc to suppress the output of the slbuild command.

evalc("slbuild(capiMdl,GenerateCodeOnly=true)");

The C API structure array that corresponds to model parameters is rtModelParameters. To view the instantiation code for this structure, examine the generated C API source file CapiConfigDemo_capi.c (located in the folder CapiConfigDemo_grt_rtw).

This is the instantiation code for the structure in this file:

/* Tunable variable parameters */
static const rtwCAPI_ModelParameters rtModelParameters[] = {
  /* addrMapIndex, varName, dataTypeIndex, dimIndex, fixPtIndex */
  { 0, TARGET_STRING("var_1"), 0, 0, 0 },

  { 0, (NULL), 0, 0, 0 }
};

The C API is generated for var_1, but not for var_2.

Now specify the storage class of var_1 as "Auto" and specify the storage class of var_2 as "Model default". Then regenerate the model code.

var_1.CoderInfo.StorageClass = "Auto";
modelSpaceVar_2.CoderInfo.StorageClass = "Model default";
evalc("slbuild(capiMdl,GenerateCodeOnly=true)");

Inspect the instantiation code for rtModelParameters again.

/* Tunable variable parameters */
static const rtwCAPI_ModelParameters rtModelParameters[] = {
  /* addrMapIndex, varName, dataTypeIndex, dimIndex, fixPtIndex */
  { 0, TARGET_STRING("var_2"), 0, 0, 0 },

  { 0, (NULL), 0, 0, 0 }
};

This time the C API is generated for var_2, but not for var_1.

Define and use MATLAB® Variables to Specify Model Parameters

MATLAB variables that are used in the model are also defined as model parameters for C API code generation.

Define two MATLAB variables, var_3 in the base workspace and var_4 in the model workspace.

var_3 = 10.27;
assignin(modelWorkSpace,var_4=2007);

To designate these two variables as model parameters, use them as the initial condition of the two Unit Delay blocks. Specify the initial condition of unitDelay1 as var_3 and specify the initial condition of unitDelay2 as var_4.

set_param(unitDelayHandle_1,InitialCondition="var_3");
set_param(unitDelayHandle_2,InitialCondition="var_4");

Specify Default Parameter Behavior as Tunable

Previously, you prevented the code generator from optimizing the parameters away by specifying their storage class as different from "Auto". You cannot do that with MATLAB variables, because they do not have storage classes. Another way to prevent the code generator from optimizing parameters away is to specify the default parameter behavior as Tunable. This turns on C API code generation for all model and block parameters in the model. For more details, see Default parameter behavior.

  1. Open the Configuration Parameters dialog box.

  2. Navigate to the Code Generation > Optimization pane.

  3. From the Default parameter behavior list, select Tunable.

  4. Click OK.

Configuration Parameters dialog box. The Code Generation > Optimization pane is open. From the Default parameter behavior list, Tunable is selected.

Alternatively, enter this in the Command Window:

set_param(capiMdl,DefaultParameterBehavior="Tunable")

Rebuild the model code.

evalc("slbuild(capiMdl,GenerateCodeOnly=true)");

Inspect the C API source file again to see the change in the C API structure for model parameters, rtModelParameters. The C API was generated for both var_3 and var_4.

/* Tunable variable parameters */
static const rtwCAPI_ModelParameters rtModelParameters[] = {
  /* addrMapIndex, varName, dataTypeIndex, dimIndex, fixPtIndex */
  { 0, TARGET_STRING("var_3"), 0, 0, 0 },

  { 1, TARGET_STRING("var_4"), 0, 0, 0 },

  { 0, (NULL), 0, 0, 0 }
};

Although var_2 remains valid in the model workspace, no C API code is generated for it. This is because it is no longer used by the model.

Related Topics