Main Content

setFunction

Set coder mapping information for model function

    Description

    setFunction(coderMapObj,funcID,Name=Value) sets code mapping information for the specified model function. Use this function to set the function customization template, memory section, or function name for a model function. For single-tasking periodic functions and Simulink® functions, you can use this function to set the argument specification, including argument names, type qualifiers, and argument order.

    example

    Examples

    collapse all

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

    To interactively observe how your commands are reflected in the Code Mappings editor, make sure the Code Mappings editor is open with the Functions tab selected. To learn how to open the Code Mappings editor, see Open the Code Mappings Editor — C.

    Open the model ECoderMapAPI.

    simulinkModel = "ECoderMapAPI";
    open_system(simulinkModel);

    Get the code mappings object of the model.

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

    Get the FunctionName property of the periodic function and the initialization function.

    periodicFcnName = getFunction(codeMapObj,"Periodic:D1","FunctionName")
    periodicFcnName = 
    'myPeriodicFcn'
    
    InitFcnName = getFunction(codeMapObj,"Initialize","FunctionName")
    InitFcnName = 
    'myInitFcn'
    

    Generate code from the model.

    evalc("slbuild(simulinkModel)");

    Entry-point functions are declared in the model header file. Store the header file name in the variable model_h_file.

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

    This is the declaration of the entry-point functions in the header file:

    /* Model entry point functions */
    extern void myInitFcn(void);
    extern void myPeriodicFcn(void);
    

    The function names are the names stored in periodicFcnName and InitFcnName.

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

    edit(model_h_file)
    

    Rename the periodic functions.

    if(startsWith(periodicFcnName,"my"))
      setFunction(codeMapObj,"Periodic:D1",FunctionName="yourPeriodicFcn");
    else
      setFunction(codeMapObj,"Periodic:D1",FunctionName="myPeriodicFcn");
    end
    if(startsWith(InitFcnName,"my"))
      setFunction(codeMapObj,"Initialize",FunctionName="yourInitFcn");
    else
      setFunction(codeMapObj,"Initialize",FunctionName="myInitFcn");
    end

    Generate code from the model again with the new entry-point function names.

    evalc("slbuild(simulinkModel)");

    This is the updated declaration of the entry-point functions in the header file.

    /* Model entry point functions */
    extern void yourInitFcn(void);
    extern void yourPeriodicFcn(void);
    

    The function names are updated.

    Input Arguments

    collapse all

    Code mapping object of the model whose function is to be set, specified as a coder.mapping.api.CodeMapping object.

    Example: myCM

    The identifier of the model function to set the code mappings property value for, specified as one of the values in the table.

    Type of Model FunctionFunction Identifier
    Initialize function

    "Initialize"

    Terminate function

    "Terminate"

    Reset function

    "Reset:slIdentifier", where slIdentifier is the name of the reset function in the model

    Periodic function

    "Periodic:slIdentifier", where slIdentifier is an annotation that corresponds to the sample time period for a periodic or continuous rate

    Example: "Periodic:D1"

    Tip

    For a single-tasking periodic function, you can use "Periodic" without :slIdentifier.

    Periodic update function

    "PeriodicUpdate:slIdentifier", where slIdentifier is an annotation that corresponds to the sample time period for a periodic or continuous rate

    Example: "PeriodicUpdate:D1".

    Tip

    For a single-tasking periodic update function, you can use "PeriodicUpdate" without :slIdentifier.

    Partition function

    "Partition:slIdentifier", where slIdentifier is a partition that was created explicitly from a block in the model and shown in the Simulink Schedule Editor

    Example: "Partition:P1"

    Partition update function

    "PartitionUpdate:slIdentifier", where slIdentifier is a partition created explicitly from a block in the model and shown in the Simulink Schedule Editor

    Example: "PartitionUpdate:P1"

    Simulink function

    "SimulinkFunction:slIdentifier", where slIdentifier is the name of the Simulink function in the model

    Exported function

    "ExportedFunction:slIdentifier", where slIdentifier is the name of the Inport block that drives the control port of the function-call subsystem, or the name of the port if it is driven by an In Bus Element block

    Tip

    When the configuration parameter Single output/update function is cleared, you can specify the update function that corresponds to a partition, periodic multitasking, or periodic single-tasking function. For more information about generating code with a single function for both update and output, see Single output/update function.

    For information about model partitioning, see Create Partitions. To learn more about annotations and sample time information, see Specify Sample Time. To learn how to programmatically obtain sample time information, see simulink.schedule.OrderedSchedule.

    Name-Value Arguments

    collapse all

    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, specified as a character vector or string scalar. If you set the default function customization template for a category of functions to Default, you can specify a memory section for the functions category.

    Data Types: char | string

    Name of a memory section that is defined in the Embedded Coder Dictionary associated with the model, specified as a character vector or string scalar.

    Data Types: char | string

    Name for the entry-point function in the generated C code, specified as a character vector or string scalar.

    Data Types: char | string

    Argument specification for the entry-point function in the generated C code, specified as a character vector or string scalar. The specification is a function prototype that shows argument names, type qualifiers, and argument order (for example, y=(u1, const *u2).

    Data Types: char | string

    String or character vector containing the name of a timer service interface defined in the Embedded Coder Dictionary. To use the dictionary default, specify "Dictionary default".

    This property is only applicable for exported functions, and only for service interface mappings. For more information, see Configure Timer Service Interfaces.

    Data Types: char | string

    Version History

    Introduced in R2020b