Main Content

Generated Code Structure for PLC_PreventExternalVarInitialization

The example shows you how to enable the PLC_PreventExternalVarInitialization option for your model, generate code and display the comparison between code generated with the PLC_PreventExternalVarInitialization option disabled and then enabled.

  1. Open the PLC_PreventExternalVarInitializationExample example:

    openExample('plccoder/PreventExternalVarInitializationExample')
    .

  2. Copy all the model files to a folder of your choice.

  3. Open the External_Var_Distributed_Codegen model.

  4. Open the Simulink PLC Coder app, and select the Subsystem block.

  5. Click Settings. Navigate to PLC Code Generation > Interface. Clear the Remove initialization statements for externally defined state variables check box.

  6. Click OK.

  7. Click Generate PLC Code.

  8. Select the Subsystem block.

  9. Click Settings. Navigate to PLC Code Generation > Interface. Set the Remove initialization statements for externally defined state variables check box.

  10. Click Generate PLC Code.

This image shows a comparison between the code generated with PLC_PreventExternalVarInitialization disabled, and then enabled. Removing initialization statements for externally defined variables prevents the corruption of their data values.

Code generated with PLC_PreventExternalVarInitialization disabled is displayed next to code generated with PLC_PreventExternalVarInitialization enabled. Code is highlighted to show need_init and IF_THEN block are not present in the code when PLC_PreventExternalVarInitialization is enabled.

See Also

Related Topics