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 modelslexVariantExportedFcn.slx. The model contains a Variant Source block with the conditionV == 1, whereVis aSimulink.Parameterobject which is defined in thePostLoadFcninFile > Model Properties > Callbacks.

The condition on the function call-signal specifies the condition forSubA. This condition is propagated to the blocks connected to the input ports ofSubAand to the blocks connected to the output ports ofSubA.

Simulate the Model

Set theVariant activation timetoupdate diagram. Before you simulate the model, in the base workspace set the value ofVto 1.

V.Value = 1;

TheSubAwill be inactive becauseV == 1. You will notice thatOut1is also inactive. However inIn2remains active becauseSubBis active andIn2is also connected toSubB. If you disconnect the line betweenIn2andSubB, thenIn2will be inactive.

Generate Code withcode compileandstartup

Generate code with theVariant activation timeset tocode compile.

rtwbuild('slexVariantExportedFcn')

View the code generation report. The report includes links to model files such asslexVariantExportedFcn.cand associated utility and header files.

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

When you change theVariant activation timefromcode compiletostartup, you must change the storage class specification ofVto'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 regularifstatements.