Main Content

Use Configuration Reference to Select Code Generation Target and Generate Testbench Code

Since R2024b

This example shows you how to use a configuration reference to select a code generation target and generate a testbench without modifying the model that you generate the code from. Use a shared configuration set when you want to:

  • Share the same configuration parameters in multiple models.

  • Change configuration parameters for multiple models without changing the model files.

  • Enable testbench code generation for multiple models without having to change the model files.

Open Model

Open the example model.

open_system("ConfigSetCodegenAndTestBench.slx");

Generate Code for CodeSys

To open the active configuration set for the model, on the Modeling tab, click Model Settings.

Because the model uses a referenced configuration, the Configuration Reference dialog box opens and displays a read-only view of the referenced configuraton set. For this model, the Referenced configuration parameter specifies the configuration hierarchy, which is the base workspace, then the configuration set.

To generate structured text code using the Simulink® PLC Coder™ app, in the top level of the model, select the Amplifier 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('ConfigSetCodegenAndTestBench/Amplifier');

Generate Code and Testbench for Schneider Electric EcoStruxure Control Expert

To generate code and a testbench for the Schneider Electric EcoStruxure Control Expert, without modifying the model, switch the configuration reference to the test bench. To switch the configuration reference, at the top of the Configuration Reference dialog box, next to Referenced configuration, select TestBench.

Because the model uses a referenced configuration, the Configuration Reference dialog box displays a read-only view of the referenced configuration set. For this model, the referenced configuration parameter specifies the configuration hierarchy, which is the base workspace, then the configuration set. The model is using the TestBench reference.

To generate Structured Text code using the Simulink® PLC Coder™ app, in the top level of the model, select the Amplifier 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('ConfigSetCodegenAndTestBench/Amplifier');

See Also

Related Topics