Contenido principal

getFunction

Get code configuration from code mappings for model function

    Description

    propertyValue = getFunction(coderMapObj,funcID,funcProperty) returns the value of a property for the specified model function. Use this function to return the function customization template or memory section configured for a model function. For single-tasking periodic functions for which you previously set an argument specification and for Simulink functions, use this function to return the argument specification.

    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 (model code mappings) returned by a call to function coder.mapping.api.get.

    Example: myCM

    The identifier of the model function to get the code mappings property value of, 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.

    Code mapping property value to return. Specify one of the property names listed in this table.

    Information to ReturnProperty Name
    Function customization template setting for the specified functionFunctionCustomizationTemplate
    Memory section associated with the specified functionMemorySection
    Name to use for the function in the generated codeFunctionName
    For periodic, single-tasking functions and Simulink functions, a string that shows the names, type qualifiers, and order of arguments as they will appear in the generated code Arguments
    Name of timer service defined in Embedded Coder DictionaryTimerService

    Example: "FunctionCustomizationTemplate"

    Output Arguments

    collapse all

    Name of the function customization template, memory section, function, or argument specification returned as a character vector or string scalar.

    Data Types: char | string

    Version History

    Introduced in R2020b