Main Content

Generate Structured Text Code By Using a Custom Supported Function List

This example shows how to generate structured text code by using a custom supported function list. Simulink® PLC Coder™ returns an error when generating code for unsupported blocks or functions. Generate structured text code for the unsupported blocks or functions by creating a custom hook file function. You must validate the generated code when using a custom hook file.

Model Description

The model consists of a subsystem block called Subsystem that accepts two integers as inputs to a MATLAB Function block. The MATLAB Function block adds the two integers together along with a double precision number generated by using the str2double function inside the function block. The str2double function is currently unsupported for PLC code generation.


Create Custom Function List

Create a custom function list by using this template file. The function name must be plc_library_function_hook. Inside the function, list the path to the function or block for which you want to generate code. To list all blocks or functions inside a toolbox or folder, use an asterisk.

 function lib_map = plc_library_function_hook(lib_map)
% register toolbox/tbx_foo/tbx_foo/lib_bar/fcn_xyz
 lib_map('toolbox/tbx_foo/tbx_foo/lib_bar/fcn_xyz.m') = {'fcn_xyz'}; 
% register all functions or blocks under toolbox/tbx_foo1/tbx_foo1/lib_bar
 lib_map('toolbox/tbx_foo1/tbx_foo1/lib_bar') = {'*'}; 

This custom hook file enables code generation for the str2double function.

type plc_library_function_hook.m
function lib_map = plc_library_function_hook(lib_map)
    lib_map('toolbox/eml/lib/matlab/strfun/str2double.m') = {'str2double'};
    lib_map('toolbox/eml/lib/matlab/strfun/char.m') = {'char'};

Place the custom function list on the MATLAB path.

Generate Structured Text Code

To generate structured text code using the Simulink® PLC Coder™ app:

  1. Open the PLC Coder app. Click Settings > PLC Code Generation. Change the Target IDE to 3S CodeSys 2.3. Click OK.

  2. Select the Subsystem block. In the PLC Code tab, click Generate PLC Code.

Alternatively, to generate structured text code from the MATLAB command line, use the plcgeneratecode function.

generatedfiles = plcgeneratecode('CustomizedMLFcn/Subsystem');
### Generating PLC code for 'CustomizedMLFcn/Subsystem'.
### Using model settings from 'CustomizedMLFcn' for PLC code generation parameters.
### Gathering test vectors for PLC testbench.
### Begin code generation for IDE codesys23.
### Emit PLC code to file.
### Creating PLC code generation report CustomizedMLFcn_codegen_rpt.html.
### PLC code generation successful for 'CustomizedMLFcn/Subsystem'.
### Generated files:

Related Topics