Main Content

Simulate a Protected Model with Code Compile or Startup Activation Time

This example shows you how to simulate a protected model in the code compile and startup variant activation times.


Open the model slexVariantMdlRefCondProp.slx. The model contains two Model blocks, sub1 and sub2. The variant condition V == 1 is propagated to the Model Block sub1. However, the condition is not propagated to the blocks within sub1. The blocks within sub1 implicitly inherit the condition. The Model block sub2 propagates the Out variant conditions.

Simulate with code compile Activation Time

Before you simulate, protect the Model block sub2. To protect the model reference of sub2, select the block sub2 and enter this command in the MATLAB Command Window. For more information on protecting models, see Protect Models to Conceal Contents (Simulink Coder).


When the Variant activation time is set to code compile, changing the active variant for variant blocks in a protected model is not supported.

Simulate with startup Activation Time

Before you simulate the model with startup activation time, delete the protected model.


Set the Variant activation time of the Variant Sink block in the Model Block sub2 to startup. Change the variant control to 'W' and change the storage class specification of W to 'ExportedGlobal'.

  W = copy(V);
  W.CoderInfo.StorageClass = 'ExportedGlobal';

Protect the model and change the active variant to simulate the model. To tune W in the protected model during simulation,

  Simulink.ModelReference.protect(gcb, 'TunableParameters', {'W'})
  W.Value = int32(3);

The condition W == 3 is propagated to the Gain6 and Out4.