Main Content

setFunctionDefault

Set default function customization template and memory section for model functions category

Since R2020b

    Description

    setFunctionDefault(coderMapObj,funcCategory,Name=Value) sets the default function customization template and memory section for the specified category of model entry-point functions.

    You cannot specify default function interfaces for models with an attached Embedded Coder Dictionary that defines a service interface configuration.

    example

    Examples

    collapse all

    Use the programmatic interface to retrieve and configure function defaults in the code mappings of a Simulink model.

    To interactively observe how your commands are reflected in the Code Mappings editor and the model dictionary, make sure the Code Mappings editor is open with the Function Defaults tab selected, and the model dictionary open with the Function Customization Template section selected. To learn how to open the Code Mappings editor, see Open the Code Mappings Editor – C. To learn how to open the model dictionary, see Open the Embedded Coder Dictionary.

    Open the model CoderMapAPI.

    simulinkModel = "ECoderMapAPI";
    open_system(simulinkModel);

    Get the code mappings object of this model.

    codeMapObj = coder.mapping.api.get(simulinkModel);

    Specify FunctionName as an empty string for all model functions so that their names in the generated code are determined by the value you set for the function defaults.

    setFunction(codeMapObj,find(codeMapObj,"Functions"),FunctionName=string.empty);

    Get the default customization template of the InitializeTerminate and Execution functions.

    exeFCNsTemplate = getFunctionDefault(codeMapObj,"Execution","FunctionCustomizationTemplate")
    exeFCNsTemplate = 
    'FCN_Template__2'
    
    initTermFCNsTemplate = getFunctionDefault(codeMapObj,"InitializeTerminate","FunctionCustomizationTemplate")
    initTermFCNsTemplate = 
    'FCN_Template__1'
    

    Generate code from the model.

    evalc("slbuild(simulinkModel)");

    Model entry-point functions are declared in the generated header file of the model. Store the header file name.

    model_h_file = fullfile(simulinkModel+"_ert_rtw",simulinkModel+".h")
    model_h_file = 
    "ECoderMapAPI_ert_rtw/ECoderMapAPI.h"
    

    Function Naming Rule of FCN_Template__1 begins with Template_1 and Function Naming Rule of FCN_Template__2 begins with Template_2. This is the entry-point function declaration section in the generated header file:

    /* Model entry point functions */
    extern void Template_1_FCN_ECoderMapAPI_initialize(void);
    extern void Template_2_FCN_ECoderMapAPI_step(void);
    extern void Template_1_FCN_ECoderMapAPI_terminate(void);
    

    The function names are generated according to the specified Function Customization Template values for those functions.

    To open the header file and, enter this command in the MATLAB® Command Window:

    edit(model_h_file)
    

    Switch between the templates of the two function categories.

    setFunctionDefault(codeMapObj,"Execution",FunctionCustomizationTemplate=initTermFCNsTemplate);
    setFunctionDefault(codeMapObj,"InitializeTerminate",FunctionCustomizationTemplate=exeFCNsTemplate);

    Generate code from the model again.

    evalc("slbuild(simulinkModel)");

    The entry-point function names are updated according to their updated Function Customization Templates:

    /* Model entry point functions */
    extern void Template_2_FCN_ECoderMapAPI_initialize(void);
    extern void Template_1_FCN_ECoderMapAPI_step(void);
    extern void Template_2_FCN_ECoderMapAPI_terminate(void);
    

    Input Arguments

    collapse all

    Code mapping object (model code mappings) returned by a call to function coder.mapping.api.get.

    Example: myCM

    Category of model entry-point functions for which to set the function customization template and memory section.

    Example: "Execution"

    Name-Value Arguments

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Example: FunctionCustomizationTemplate="exFastFunction"

    Name of a function customization template defined in the Embedded Coder Dictionary associated with the model. If you set the default function customization template for a category of functions to Default, you can specify a memory section for the category of functions.

    Data Types: char | string

    Name of a memory section that is defined in the Embedded Coder Dictionary associated with the model.

    Data Types: char | string

    Version History

    Introduced in R2020b