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
, whereV
is aSimulink.Parameter
object which is defined in thePostLoadFcn
inFile > 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 time
toupdate diagram
. Before you simulate the model, in the base workspace set the value ofV
to 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 compile
andstartup
Generate code with theVariant activation time
set tocode compile
.
rtwbuild('slexVariantExportedFcn')
View the code generation report. The report includes links to model files such asslexVariantExportedFcn.c
and associated utility and header files.
The figure contains a portion of the generated code,slexVariantExportedFcn.c
. You will notice thatIn1
is guarded, internally and externally, by the preprocessor conditionals(#if
).In3
is void because it does not receive any condition.
When you change theVariant activation time
fromcode compile
tostartup
, you must change the storage class specification ofV
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 regularif
statements.