Main Content

setFunction

Set coder mapping information for model function

Since R2020b

    Description

    setFunction(coderMapObj,modelFunc,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

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

    Example: myCM

    Model function for which to return a code mapping property value, specified as one of these values:

    Specified ValueType of Model Function
    "ExportedFunction:slIdentifier", where slIdentifier is the name of the function-call Inport block in the model Exported function
    "Initialize"Initialize function
    "Partition:slIdentifier", where slIdentifier is a partition created explicitly from a block in the model and shown in the Simulink Schedule Editor (for example, P1), or a task name in the Concurrent Execution dialog boxPartition function
    "PartitionUpdate:slIdentifier", where slIdentifier is a partition created explicitly from a block in the model and shown in the Simulink Schedule Editor (for example, P1), or a task name in the Concurrent Execution dialog boxPartition update function
    "Periodic:slIdentifier", where slIdentifier is an annotation that corresponds to the sample time period for a periodic or continuous rate of a multitasking model (for example, D1) Periodic multitasking function
    "PeriodicUpdate:slIdentifier", where slIdentifier is an annotation that corresponds to the sample time period for a periodic or continuous rate of a multitasking model (for example, D1) Periodic multitasking update function
    "Periodic"Periodic single-tasking function
    "PeriodicUpdate"Periodic single-tasking update function
    "Reset:slIdentifier", where slIdentifier is the name of the reset function in the model Reset function
    "SimulinkFunction:slIdentifier", where slIdentifier is the name of the Simulink function in the modelSimulink function
    "Terminate"Terminate function

    If model configuration parameter Single output/update function is cleared, you can specify the updated version of the partition, periodic multitasking or periodic single-tasking function. For information about model partitioning, see Create Partitions.

    Example: "Periodic:D1"

    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, 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