Main Content

Configure Model to Generate C API Code for Block Parameters

This example shows how to configure the model to include the C API for block 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)

Specify Default Parameter Behavior as Tunable

To prevent optimizing away block parameters, specify the default parameter behavior as Tunable (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")

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)");

Verify That Block Parameters Are Included in C API

The C API structure array that corresponds to block parameters is rtBlockParameters. 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:

static const rtwCAPI_BlockParameters rtBlockParameters[] = {
  /* addrMapIndex, blockPath,
   * paramName, dataTypeIndex, dimIndex, fixPtIdx
   */
  { 0, TARGET_STRING("CapiConfigDemo/unitDelay1"),
    TARGET_STRING("InitialCondition"), 0, 0, 0 },

  { 1, TARGET_STRING("CapiConfigDemo/unitDelay2"),
    TARGET_STRING("InitialCondition"), 0, 0, 0 },

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

Related Topics