Main Content

Create Custom Code Generation Objectives

The Code Generation Advisor reviews your model based on objectives that you specify. If the predefined efficiency, traceability, Safety precaution, and debugging objectives do not meet your requirements, you can create custom objectives.

To create custom objectives:

  • Create an objective and add parameters and checks to this new objective.

  • Create an objective based on an existing objective, then add, modify, and remove the parameters and checks within that new objective.

Specify Parameters in Custom Objectives

When you create a custom objective, you specify the values of configuration parameters that the Code Generation Advisor reviews. You can use the following methods:

  • addParam — Add parameters and specify the values that the Code Generation Advisor reviews in Check model configuration settings against code generation objectives.

  • modifyInheritedParam — Modify inherited parameter values that the Code Generation Advisor reviews in Check model configuration settings against code generation objectives.

  • removeInheritedParam — Remove inherited parameters from a new objective that is based on an existing objective. When you select multiple objectives, if another selected objective includes this parameter, the Code Generation Advisor reviews the parameter value in Check model configuration settings against code generation objectives.

Specify Checks in Custom Objectives

Objectives include the Check model configuration settings against code generation objectives check by default. When you create a custom objective, you specify the list of additional checks that are associated with the custom objective. You can use the following methods:

  • addCheck — Add checks to the Code Generation Advisor. When you select the custom objective, the Code Generation Advisor displays the check, unless you specify an additional objective with a higher priority that excludes the check.

    For example, add a check to the Code Generation Advisor to include a custom check in the automatic model checking process.

  • excludeCheck — Exclude checks from the Code Generation Advisor. When you select multiple objectives, if you specify an additional objective that includes this check as a higher priority objective, the Code Generation Advisor displays this check.

    For example, exclude a check from the Code Generation Advisor when a check takes a long time to process.

  • removeInheritedCheck — Remove inherited checks from a new objective that is based on an existing objective. When you select multiple objectives, if another selected objective includes this check, the Code Generation Advisor displays the check.

    For example, remove an inherited check, rather than exclude the check, when the check takes a long time to process, but the check is important for another objective.

Determine Checks and Parameters in Existing Objectives

When you base a new objective on an existing objective, you can determine what checks and parameters the existing objective contains. The Code Generation Advisor contains the list of checks in each objective.

For example, the Efficiency objective includes checks that you can see in the Code Generation Advisor.

  1. Open the EmbeddedCoderIntro model.

    openExample('EmbeddedCoderIntro')
  2. Specify an ERT-based target.

  3. On C Code tab, click C/C++ Code Advisor.

  4. In the System Selector window, select the model or subsystem that you want to review, and then click OK.

  5. In the Code Generation Advisor, on the Code Generation Objectives pane, select the code generation objectives. As you select objectives, on the left pane, the Code Generation Advisor updates the list of checks it runs on your model. For this example, select Execution efficiency. In Available objectives, double-click Execution efficiency. Execution efficiency is added to Selected objectives - prioritized.

In the left pane, the Code Generation Advisor lists the checks for the Execution efficiency objective. The first check, Check model configuration settings against code generation objectives, lists parameters and values specified by the objective. For example, the Code Generation Advisor displays the list of parameters and the recommended values in the Execution efficiency objective. To see the list of parameters and values:

  1. Run Check model configuration settings against code generation objectives.

  2. Click Modify Parameters.

  3. Rerun the check.

In the check results, the Code Generation Advisor displays the list of parameters and recommended values for the Execution efficiency objective.

Steps to Create Custom Objectives

To create a custom objective:

  1. Create an sl_customization.m file.

    • Specify custom objectives in a single sl_customization.m file only or the software generates an error. This issue is true even if you have more than one sl_customization.m file on your MATLAB® path.

    • Except for the matlabroot/work folder, do not place an sl_customization.m file in your root MATLAB folder or its subfolders. Otherwise, the software ignores the customizations that the file specifies.

  2. Create an sl_customization function that takes a single argument. When the software invokes the function, the value of this argument is the Simulink® customization manager. In the function:

    • To create a handle to the code generation objective, use the ObjectiveCustomizer constructor.

    • To register a callback function for the custom objectives, use the ObjectiveCustomizer.addCallbackObjFcn method.

    • To add a call to execute the callback function, use the ObjectiveCustomizer.callbackFcn method.

    For example:

    function sl_customization(cm)
    %SL_CUSTOMIZATION objective customization callback
    
    objCustomizer = cm.ObjectiveCustomizer;
    index = objCustomizer.addCallbackObjFcn(@addObjectives);
    objCustomizer.callbackFcn{index}();
    
    end

  3. Create a MATLAB callback function that:

    The following example shows how to create an objective Reduce RAM Example. Reduce RAM Example includes five parameters and three checks that the Code Generation Advisor reviews.

    function addObjectives
    
    % Create the custom objective
    obj = rtw.codegenObjectives.Objective('ex_ram_1');
    setObjectiveName(obj, 'Reduce RAM Example');
    
    % Add parameters to the objective
    addParam(obj, 'DefaultParameterBehavior', 'Inlined');
    addParam(obj, 'BooleanDataType', 'on');
    addParam(obj, 'OptimizeBlockIOStorage', 'on');
    addParam(obj, 'EnhancedBackFolding', 'on');
    addParam(obj, 'BooleansAsBitfields', 'on');
    
    % Add additional checks to the objective
    % The Code Generation Advisor automatically includes 'Check model
    % configuration settings against code generation objectives' in every
    % objective.
    addCheck(obj, 'mathworks.design.UnconnectedLinesPorts');
    addCheck(obj, 'mathworks.design.Update');
    
    %Register the objective
    register(obj);
    
    end

    The following example shows you how to create an objective My Traceability Example based on the existing Traceability objective. The custom objective modifies, removes, and adds parameters that the Code Generation Advisor reviews. It also adds and removes checks from the Code Generation Advisor.

    function addObjectives
    
    % Create the custom objective from an existing objective
    obj = rtw.codegenObjectives.Objective('ex_my_trace_1', 'Traceability');
    setObjectiveName(obj, 'My Traceability Example');
    
    % Modify parameters in the objective
    modifyInheritedParam(obj, 'GenerateTraceReportSf', 'Off');
    removeInheritedParam(obj, 'ConditionallyExecuteInputs');
    addParam(obj, 'MatFileLogging', 'On');
    
    % Modify checks in the objective
    addCheck(obj, 'mathworks.codegen.SWEnvironmentSpec');
    removeInheritedCheck(obj, 'mathworks.codegen.CodeInstrumentation');
    
    %Register the objective
    register(obj);
    
    end

  4. If you previously opened the Code Generation Advisor, close the model from which you opened the Code Generation Advisor.

  5. Refresh the customization manager. At the MATLAB command line, enter sl_refresh_customizations.

  6. Refresh the advisor check information cache. At the MATLAB command line, enter Advisor.Manager.refresh_customizations().

  7. Open your model and review the new objectives.

Related Topics