MATLAB: How to conditionally disable Simulink model callbacks during a model build in Simulink 7.9 (R2012a)

simulink

I have a parent model "topLevelModel" and a referenced model "gainRefModel." The referenced model's PreLoadFcn and InitFcn callbacks contain the line of code
Param.gainRefModel.Gain = 5.0
for setting the gain value of a Gain block. I have also created a build function for changing the Param structure to a Simulink.Parameter object before issuing the rtwbuild command. As the model compiles, I receive the error
Error evaluating 'InitFcn' callback of block_diagram 'gainRefModel'.
I understand this arises because the Init callback is executed as the model compiles, and there is an assignment error because there is no field called gainRefModel for class Simulink.Parameter. I cannot, however, remove the contents of the Init callback for simulation purposes. Is there is a way to tell Simulink to ignore the InitFcn callback only during builds?

Best Answer

Unfortunately, there isn't a way to ignore the model callbacks when building a model. A workaround would be to declare Param as a Simulink.Parameter object from the start. The gain field of the Gain block can still be left as Param.gainRefModel.Gain. The only change necessary is to modify the content of the callback to be "Param.Value.gainRefModel.Gain = 5.0" (with the addition of the .Value field).