Main Content

addGlobalArg

Selectively add global variable from custom code to C Caller block as global argument

Since R2024b

    Description

    GlobalArg = callerObj.addGlobalArg(globalVariableName) adds a variable declared as global in the custom code to a C Caller block as a global argument. callerObj is an object of class FunctionPortSpecification. The function also creates a FunctionArgument object. To use this function in Simulink®, navigate to Configuration Parameters > Simulation Target > Import settings and ensure that the Automatically infer global variables as function interfaces checkbox is not selected. Alternatively, from the command line, use set_param function and set CustomCodeGlobalsAsFunctionIO to off for the corresponding block. For more information, see Automatically infer global variables as function interfaces.

    example

    Examples

    collapse all

    This function enables you to selectively add global variables from custom code to a C Caller block as global argument.

    Replace mdlName with the name of your model. Using this command, you can check whether the Automatically infer global variables as function interfaces parameter is selected or not selected.

    get_param(mdlName,'CustomCodeGlobalsAsFunctionIO')

    If the previous step returns 'off', skip this step and move to the next one.

    If the previous step returns 'on', use this command to disable automatic addition of global variables as global arguments. This enables you to use addGlobalArg function to add the global variables you want to add.

    set_param(mdlName,'CustomCodeGlobalsAsFunctionIO','off');

    Then obtain FunctionPortSpecification object of the C Caller block in your model. To do so, select the block in your model canvas and enter this command at the MATLAB® command line.

    CCallerObj = get_param(gcb,'FunctionPortSpecification');

    Use the addGlobalArg function to add global variables to the block. You can replace myGlobalVar with a global variable you want to use in your model.

    myGlobalArg = CCallerObj.addGlobalArg('myGlobalVar');

    You can view and edit the properties of the global arguments in the Port specification table of the C Caller block.

    Input Arguments

    collapse all

    Name of the global variable to add to the block, specified as a character vector or string scalar.

    Data Types: char | string

    Output Arguments

    collapse all

    Global argument added to the C Caller block, returned as a FunctionArgument object.

    Version History

    Introduced in R2024b