Main Content

Generate Code from an Export Function Model

This example shows how to simulate and generate code for an export function model.

Model

Open the model slexVariantExportedFcn.slx. The model contains a Variant Source block with the condition V == 1, where V is a Simulink.Parameter object which is defined in the PostLoadFcn in File > Model Properties > Callbacks.

The condition on the function call-signal specifies the condition for SubA. This condition is propagated to the blocks connected to the input ports of SubA and to the blocks connected to the output ports of SubA.

Simulate the Model

Set the Variant activation time to update diagram. Before you simulate the model, in the base workspace set the value of V to 1.

  V.Value = 1;

The SubA will be inactive because V == 1. You will notice that Out1 is also inactive. However in In2 remains active because SubB is active and In2 is also connected to SubB. If you disconnect the line between In2 and SubB, then In2 will be inactive.

Generate Code with code compile and startup

Generate code with the Variant activation time set to code compile.

rtwbuild('slexVariantExportedFcn')

View the code generation report. The report includes links to model files such as slexVariantExportedFcn.c and associated utility and header files.

The figure contains a portion of the generated code, slexVariantExportedFcn.c. You will notice that In1 is guarded, internally and externally, by the preprocessor conditionals(#if). In3 is void because it does not receive any condition.

When you change the Variant activation time from code compile to startup, you must change the storage class specification of V to 'ExportedGlobal'.

  V.CoderInfo.StorageClass = 'ExportedGlobal';
  rtwbuild('slexVariantExportedFcn')

View the code generation report. The figure contains a portion of the generated code, slexVariantExportedFcn.c. You will notice that the generated code contains the regular if statements.