Main Content

Configure and Optimize Model with Configuration Wizard Blocks

The Embedded Coder® software provides a library of Configuration Wizard blocks and scripts to help you configure and optimize code generation from your models.

Configuration Wizard Block Library

The library provides a Configuration Wizard block that you can customize. It also provides four preset Configuration Wizard blocks that update the active configuration parameters for a specified goal.

BlockDescription
Custom MATLAB fileUpdate active configuration parameters of parent model by using a custom file
ERT (optimized for fixed-point)Update active configuration parameters of parent model for ERT fixed-point code generation
ERT (optimized for floating-point)Update active configuration parameters of parent model for ERT floating-point code generation
GRT (debug for fixed/floating-point)Update active configuration parameters of parent model for GRT fixed- or floating-point code generation with debugging enabled
GRT (optimized for fixed/floating-point)Update active configuration parameters of parent model for GRT fixed- or floating-point code generation

When you add one of the preset Configuration Wizard blocks to your model and double-click it, a predefined MATLAB® file script configures parameters of the active configuration set without manual intervention. The preset blocks optimally configure the parameters for one of the following cases:

  • Fixed-point code generation with the ERT target

  • Floating-point code generation with the ERT target

  • Fixed-point or floating-point code generation with TLC debugging parameters enabled, with the GRT target.

  • Fixed-point or floating-point code generation with the GRT target

The Custom block provides an example MATLAB file script that you can adapt to your requirements.

You can also set up the Configuration Wizard blocks to invoke the build process after configuring the model.

Add a Configuration Wizard Block

The Configuration Wizard blocks are available in the Embedded Coder block library. To use a Configuration Wizard block:

  1. Open the model that you want to configure.

  2. Open the Embedded Coder block library by typing the command rtweclib.

  3. Double-click the Configuration Wizards icon. The Configuration Wizards sublibrary opens.

  4. Select the Configuration Wizard block that you want to use and drag it into your model. This model contains the ERT (optimized for fixed-point) Configuration Wizard block.

  5. If you want the Configuration Wizard block to invoke the build process after configuration, right-click the Configuration Wizard block in your model, and select Mask > Mask Parameters from the context menu. Then, select the Invoke build process after configuration parameter. Do not change the Configure the model for block parameter, unless you want to create a custom block and script. In that case, see Create a Custom Configuration Wizard Block.

  6. Click Apply and close the Mask Parameters dialog box.

  7. Save the model.

Use Configuration Wizard Blocks to Configure Your Model

After you add a Configuration Wizard block to your model, to configure your model, double-click the block. The script associated with the block sets parameters of the active configuration set that are relevant to code generation (including selection of the target). You can see that the parameters have changed by opening the Configuration Parameters dialog box and examining the parameter settings.

If you selected the Invoke build process after configuration block parameter, the script also initiates the code generation and build process.

Note

To provide a quick way to switch between configurations, you can add more than one Configuration Wizard block to your model.

Create a Custom Configuration Wizard Block

The Custom Configuration Wizard block and the associated MATLAB file script, matlabroot/toolbox/coder/simulinkcoder_core/rtwsampleconfig.m, provide a starting point for customization.

Set Up a Configuration Wizard Block

Set up a custom Configuration Wizard block and link it to a script. If you want to use the block in more than one mode, it is advisable to create a Simulink® library to contain the block.

To begin, make a copy of the example script for later customization:

  1. To store your custom script, create a folder. This folder must not be anywhere inside the MATLAB folder structure (that is, it must not be under matlabroot).

    The example refers to this folder as /my_wizards.

  2. Add the folder to the MATLAB path. Save the path for future sessions.

  3. Copy the example script rtwsampleconfig.m in the folder matlabroot/toolbox/rtw/rtw (open) to the /my_wizards folder that you created. Then, rename the script. This example uses the name my_configscript.m.

  4. Open the example script into the MATLAB editor. Scroll to the end of the file and enter the following line of code:

    disp('Custom Configuration Wizard Script completed.');

    This statement is used later as a test to see that your custom block has executed the script.

  5. Save your script and close the MATLAB editor.

The next task is to create a Simulink library and add a custom block to it.

  1. Open the Embedded Coder block library and the Configuration Wizards sublibrary, as described in Add a Configuration Wizard Block.

  2. On the Library tab, select New > Library. From the Simulink Start Page, choose a Blank Library. An empty library window opens.

  3. Select the Custom MATLAB file block from the Configuration Wizards sublibrary and drag it into the empty library window.

  4. To distinguish your custom block from the original, edit the Custom MATLAB file label under the block.

  5. On the Library tab, select Save > Save as. Save the library to the /my_wizards folder, under your library name of choice. In this figure, the library is saved as ex_custom_button and the block is labeled my_wizard MATLAB-file.

The next task is to link the custom block to the custom script:

  1. Right-click the block in your model and select Mask > Mask Parameters from the context menu. The Configure the model for menu is set to Custom. When Custom is selected, the Configuration function edit field is enabled so that you can enter the name of a custom script.

  2. In the Configuration function field, enter the name of your custom script. (Do not enter the .m file name extension, which is implicit.)

  3. By default, the Invoke build process after configuration parameter is cleared. You can change the default for your custom block by selecting this option. For now, leave this parameter cleared.

  4. Click Apply and close the Mask Parameters dialog box.

  5. Save the library.

  6. Close the Embedded Coder block library and the Configuration Wizards sublibrary. Leave your custom library open for use in the next task.

Test your block and script in a model.

  1. Create or open a model that has model configuration parameter System target file set to grt.tlc.

  2. Select your custom block from your custom library. Drag the block into the model that you created or opened.

  3. In the model, double-click your custom block.

  4. In the MATLAB window, you see the test message that you previously added to your script:

    Custom Configuration Wizard Script completed.

    The test message indicates that the custom block executed the script.

  5. Check the setting of the System target file parameter. The parameter should be set to ert.tlc.

Before applying further edits to your custom script, proceed to the next section to learn about the operation and conventions of Configuration Wizard scripts.

Create a Configuration Wizard Script

Create your custom Configuration Wizard script by copying and modifying the example script, rtwsampleconfig.m.

The Configuration Function.  The example script implements a single function without a return value. The function takes a single argument cs:

function rtwsampleconfig(cs)

The argument cs is a handle to a proprietary object that contains information about the active configuration set. The Simulink software obtains this handle and passes it in to the configuration function when you double-click a Configuration Wizard block.

Your custom script must conform to this prototype. Your code must use cs as a “black-box” object that transmits information to and from the active configuration set.

Access Configuration Set Parameters.  To set parameters or obtain parameter values, use the Simulink set_param and get_param functions.

Option names are passed in to set_param and get_param as character vectors specifying an internal option name. The internal option name can be different from the option label on the UI (for example, the Configuration Parameters dialog box). The example configuration accompanies each set_param and get_param call with a comment that correlates internal option names to UI option labels. For example:

set_param(cs,'LifeSpan','1'); % Application lifespan (days)

To obtain the current setting of an option in the active configuration set, call get_param. Pass in the cs object as the first argument, followed by the internal option name. For example, the following code excerpt tests the setting of the Create code generation report option:

if strcmp(get_param(cs, 'GenerateReport'), 'on')
    ...

To set an option in the active configuration set, call set_param. Pass in the cs object as the first argument, followed by one or more parameter/value pairs that specify the internal option name and its value. For example, the following code excerpt turns off the Support absolute time option:

set_param(cs,'SupportAbsoluteTime','off');

Select a Target.  A Configuration Wizard script must select a target configuration. The example script uses the ERT target as a default. The script first stores character vector variables that correspond to the required System target file, Template makefile, and Make command settings:

stf = 'ert.tlc';
tmf = 'ert_default_tmf';
mc  = 'make_rtw';

You select the system target file by passing the cs object and the stf character vector to the switchTarget function:

switchTarget(cs,stf,[]);

Set the template makefile and make command options by using set_param calls:

set_param(cs,'TemplateMakefile',tmf);
set_param(cs,'MakeCommand',mc);

To select a target, your custom script must set up the character vector variables stf, tmf, and mc and pass them to the calls.

Obtain Target and Configuration Set Information.  The following utility functions and properties are provided so that your code can obtain information about the current target and configuration set with the cs object:

  • isValidParam(cs, 'option'): The option argument is an internal option name. isValidParam returns true if option can be accessed by using the get_param function in the context of the active configuration set.

  • getPropEnabled(cs, 'option'): The option argument is an internal option name. Returns true if this option is enabled (that is, writable).

  • IsERTTarget property: Your code can detect whether the currently selected target is derived from the ERT target by checking the IsERTTarget property, as follows:

      isERT = strcmp(get_param(cs,'IsERTTarget'),'on');

You can use this information to determine whether the script must configure ERT-specific parameters, for example:

if isERT
  set_param(cs,'ZeroExternalMemoryAtStartup','off');
  set_param(cs,'ZeroInternalMemoryAtStartup','off');
  set_param(cs,'InitFltsAndDblsToZero','off');
  set_param(cs,'NoFixptDivByZeroProtection','on')
end

Invoke a Configuration Wizard Script from the Command Line

Configuration Wizard scripts can be run from the MATLAB command line.

Before invoking the script, you must open a model and instantiate a cs object to pass in as an argument to the script. After running the script, you can invoke the build process with the slbuild command. The following example opens, configures, and builds a model.

open my_model;
cs = getActiveConfigSet ('my_model');
rtwsampleconfig(cs);
slbuild('my_model');

Related Topics