Main Content

Integrate Custom Function Block in Generated Code

To integrate a custom function block, ExternallyDefinedBlock, this procedure uses the example plcdemo_external_symbols. To open the example, enter:


  1. In a Simulink® model, add a MATLAB Function block.

  2. Double-click the MATLAB Function block.

  3. In the MATLAB® editor, minimally define inputs, outputs, and stubs. For example:

    Functions that have only one output, no states, and do not access global variables are generated as FUNCTION in the generated structured text code.

    function Y = fcn(U,V)
    % Stub behavior for simulation. This block 
    % is replaced during code generation
    Y = U + V;
  4. Change the MATLAB Function block name to ExternallyDefinedBlock.

  5. Create a subsystem from this MATLAB Function block.

  6. Complete the model to look like the Subsystem block in


  7. Open the Configuration Parameters dialog box for the model.

  8. Add ExternallyDefinedBlock to PLC Code Generation > Identifiers > Externally Defined Identifiers.

  9. The plcdemo_external_symbols model also suppresses K1 and InBus. Add these symbol names to the Externally Defined Identifiers field, separated by spaces or commas. For other settings, see the plcdemo_external_symbols model.

  10. Save and close your new model. For example, save it as plcdemo_external_symbols_mine.

  11. Generate code for the model.

  12. In the generated code, look for instances of ExternallyDefinedBlock.

    The reference of ExternallyDefinedBlock is:

    The omission of ExternallyDefinedBlock is: