Main Content

addGlobalArg

R2026b

Class: Simulink.CustomCode.FunctionPortSpecification
Namespace: Simulink.CustomCode

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

Since R2024b

Description

GlobalArg = CCallerObj.addGlobalArg(globalVariableName) adds a global variable defined in the custom code as a global argument to a C Caller block. The CCallerObj 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 check that the Automatically infer global variables as function interfaces check box is not selected. Alternatively, from the command line, use the set_param function and set CustomCodeGlobalsAsFunctionIO to off for the corresponding block. For more information, see Automatically infer global variables as function interfaces.

example

Input Arguments

expand 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

expand all

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

Examples

expand all

Selectively add global variables from custom code to a C Caller block as global arguments.

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 get_param function returns 'off', skip the next step.

If the get_param returns 'on', use this command to disable automatic addition of global variables as global arguments. This settings allows you to use the addGlobalArg function to select the global variables you want to add.

set_param(mdlName,CustomCodeGlobalsAsFunctionIO = "off");

Obtain a FunctionPortSpecification object from the C Caller block (indicated as gcb) in your model using the get_param function. Select the block in your model canvas and enter this command at the MATLAB® command line.

CCallerObj = get_param(gcb,'FunctionPortSpecification');

Use the addGlobalArg method 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.

Version History

Introduced in R2024b