Main Content

MATLAB Function Block Simulink PLC Coder Structured Text Code Generation

Configuring the rand function for PLC Code Generation

Simulink® PLC Coder™ generates structured text code for MATLAB® Function blocks and Stateflow® charts that use the MATLAB rand function. You implement the rand function by using a pseudo random number generator that works with PLC IDEs supporting the uint32 data type. The software has conformance checks to report diagnostics for incompatible targets. These targets have been tested for rand function support.

  • 3S-Smart Software Solutions CODESYS Version 2.3 or 3.3 or 3.5 (SP4 or later)

  • B&R Automation Studio® 3.0 or 4.0

  • Beckhoff® TwinCAT® 2.11 or 3

  • OMRON® Sysmac® Studio Version 1.04, 1.05, 1.09 or 1.12

  • Rexroth IndraWorks version 13V12 IDE

  • PLCopen XML

SimulinkWidth Block Requirements for PLC Code generation

Instead of using the Simulink Width block , inside the MATLAB Functionuse the MATLAB length function to compute the input vector width.

Workspace Parameter Data Type Limitations

If the data type of the MATLAB work space parameter value does not match that of the block parameter in your model, the value of the variable in the generated code is set to zero.

If you specify the type of the Simulink.Parameter object by using the DataType property, use a typed expression when assigning a value to the parameter object. For example, if the Simulink.Parameter object K1 stores a value of the type single, use a typed expression such as single(0.3) when assigning a value to K1.

K1 = Simulink.Parameter;
K1.Value = single(0.3);
K1.StorageClass = 'ExportedGlobal';
K1.DataType = 'single';


When generating structured text from MATLAB Function blocks, these are the limitations :

  • Cell arrays in MATLAB Function blocks are not supported.

  • If you want to use a function from a toolbox within the MATLAB Function block, you must check the toolbox function page to see if that block supports code generation from Simulink PLC Coder.

  • When generating a testbench for models that use the rand function , different rand output values may be generated when gathering test vectors vs code generation, leading to testbench verification failures . To prevent these failures make sure that the rand output value remains constant across different model compilations.