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:
openExample('plcdemo_external_symbols');
In a Simulink® model, add a MATLAB Function block.
Double-click the MATLAB Function block.
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;
Change the MATLAB Function block name to ExternallyDefinedBlock.
Create a subsystem from this MATLAB Function block.
Complete the model to look like the Subsystem block in
.openExample('plcdemo_external_symbols');
Open the Configuration Parameters dialog box for the model.
Add
ExternallyDefinedBlock
to PLC Code Generation > Identifiers > Externally Defined Identifiers.The
plcdemo_external_symbols
model also suppressesK1
andInBus
. Add these symbol names to the Externally Defined Identifiers field, separated by spaces or commas. For other settings, see theplcdemo_external_symbols
model.Save and close your new model. For example, save it as
plcdemo_external_symbols_mine
.Generate code for the model.
In the generated code, look for instances of
ExternallyDefinedBlock
.The reference of
ExternallyDefinedBlock
is:The omission of
ExternallyDefinedBlock
is: