Main Content

Configure Model to Generate C API Code

Configure your model to include C API elements in the generated code. The C API layer is an extension of the generated code, so you can configure the C API generation only for elements that exist in the generated code. To configure an element to generate C API code, you need to:

  • Make sure the element is not configured to be optimized away, so it exists in the generated code.

  • Apply additional configuration settings that are required for generating C API code for the element type.

This table summarizes the required configuration for each type of model element:

Model Element TypeApplies ToRequired ConfigurationExample Link
Root-level portsOutput signals of root-level input port blocks and input signals of root-level output port blocksIn the Configuration Parameters dialog box, enable C API code generation for root-level ports (see Generate C API for: root-level I/O). Configure Model to Generate C API Code for Root-Level Ports
Model signalsOutput signals of blocks that are not root-level input port blocks

In the Configuration Parameters dialog box, enable C API code generation for signals (see Generate C API for: signals).

To prevent the code generator from optimizing the signal away, do one of these:

Configure Model to Generate C API Code for Signals
Model statesModel elements whose value in an execution step of the model depends on its value in the previous model execution step

In the Configuration Parameters dialog box, enable C API code generation for states (see Generate C API for: states).

Configure Model to Generate C API Code for States
Model parameters

Any variables that are used by the model. The variables can be:

  • MATLAB® variables or Simulink.Parameter variables

  • Defined in the base workspace or the model workspace

In the Configuration Parameters dialog box, enable C API code generation for parameters (see Generate C API for: parameters).

To prevent the code generator from optimizing the parameter away, do one of these:

  • Specify the storage class of the parameter as a value other than Auto.

  • In the Configuration Parameters dialog box, specify the default parameter behavior as tunable (see Default parameter behavior).

Configure Model to Generate C API Code for Model Parameters
Block parametersValues of block parameters
  • In the Configuration Parameters dialog box, enable C API code generation for parameters (see Generate C API for: parameters).

  • To prevent the code generator from optimizing the parameter away, in the Configuration Parameters dialog box, specify the default parameter behavior as Tunable (see Default parameter behavior).

Configure Model to Generate C API Code for Block Parameters

Some model elements belong to multiple types of C API elements and therefore you can access them in multiple ways. For example, a signal line that connects a Gain block and an Outport block can be accessed as a model signal or as a root-level output port signal.

Related Topics