Argument Specification for Function Caller Blocks
A Simulink® function is considered out of scope of its corresponding Function Caller block when the function is defined outside the current model hierarchy. In this case, the data types and dimensions of the input and output arguments are unknown to the Function Caller block and must be specified. You can specify the data types and dimensions using the Input argument specifications and Output argument specifications parameters of the Function Caller block.
Note
When calling a function through a function port that uses a
Simulink.ServiceBus data type, the input and output argument
specifications are defined by the Simulink.ServiceBus and therefore do not
have to be specified. In the case that an output argument is a Simulink.Bus
object, you must set the Output argument specifications
parameter.
The following sections provide examples of expressions for Input argument specifications and Output argument specifications parameters for Function Caller blocks when the Simulink Function block is out of scope.
Specify Data Types for Input and Output Arguments
The table provides examples of input and output argument specification expressions for Function Caller blocks based on data types of its corresponding Simulink Function block.
| Simulink Function Block Data Type | Function Caller Block Expression | Description |
|---|---|---|
double | double(1.0) | Double-precision scalar. |
double | double(ones(12,1)) | Double-precision column vector of length 12. |
single | single(1.0) | Single-precision scalar. |
int8, int16,
int32 | int8(1), int16(1),
int32(1) | Integer scalars. |
int32([1 1 1]) | Integer row vector of length 3. | |
int32(1+1i) | Complex scalar whose real and imaginary parts are 32-bit integers. | |
uint8, int16,
int32 | uint8(1), uint16(1),
uint32(1) | Unsigned integer scalars. |
boolean | boolean(true),boolean(false) | Boolean, initialized to true (1) or false (0). |
|
| 16-bit fixed-point signed scalar with binary point set to zero. Fixed-point numbers can have a word size up to 65,535 bits. |
fixdt(1,16,4) | fi(0,1,16,4) | 16-bit fixed-point signed scalar with binary point set to 4. |
fixdt(1,16,2^0,0) | fi(0,1,16,2^0,0) | 16-bit fixed-point signed scalar with slope set to 2^0 and
bias set to 0. |
Bus: <object name> | bus object name | Simulink.Bus object |
Enum: <class name> | enumerated value | Enumerated type set to an enumerated value. |
<alias name> | parameter object name | Simulink.Parameter object with the
DataType parameter set to a Simulink.AliasType object and the
Value parameter set to a value. |
<signal name> | signal object name | Simulink.Signal object with unbound
variable-size dimensions. |
Specify Enumerated Data Type for Input and Output Arguments
This example shows how to specify the input arguments of a Function Caller block as an enumerated data type.
Create an enumerated data type for the three primary colors, and then specify the Input argument specifications parameter for a Function Caller block. The Function Caller block calls a Simulink Function block that accepts a signal with the enumerated type as input.
Create a MATLAB® file for saving the data type definition. On the MATLAB toolstrip, select New > Class.
In the MATLAB editor, define the elements of an enumerated data type. The class
BasicColorsis a subclass of the classSimulink.IntEnumType.classdef BasicColors < Simulink.IntEnumType enumeration Red(0) Yellow(1) Blue(2) end endSave the class definition in a file named
BasicColors.m.In the Function Caller block dialog box, set the Input argument specifications to
BasicColors(0).From the Argument Inport block within the Simulink Function block, in the block dialog box, set the Data type parameter to
Enum: BasicColors.
Specify Alias Data Type for Input and Output Arguments
This example shows how to specify the input arguments of a Function Caller block as an alias data type.
Create an alias name for the data type single, and then specify the Input argument specification parameter for a Function Caller block. The Simulink Function block called by the Function Caller block also uses the alias name to define the input data type.
Create a Simulink alias data type object
myAlias.myAlias = Simulink.AliasType;
Assign a data type.
myAlias.BaseType = 'single';
Create a Simulink parameter object
myAlias_parameterand assign the alias name to theDataTypeparameter.myAlias_parameter = Simulink.Parameter; myAlias_parameter.DataType = 'myAlias'; myAlias_parameter.Value = 1;
In the Function Caller block dialog box, set the Input argument specification parameter to
myAlias_parameter.From the Argument Inport block within the Simulink Function block, in the block dialog box, set the Data type parameter to
myAlias.
Specify Unbounded Variable-Size Signals as Data Type for Input and Output Arguments
Since R2023b
This example shows how to specify the input and output arguments of a Function
Caller block as unbounded variable-size signals. A Function Caller
block only accepts Simulink.Signal objects that represent unbounded
variable-size signals. For more information, see Unbounded Variable-Size Signals.
The following steps show how to specify an unbounded variable-size signal as input argument for a Function Caller block and how to configure the related Simulink Function block.
Create a
Simulink.Signalobject namedmySigthat represents the input signal. Load this signal object into the model using the model callback parameter PreLoadFcn. To use the PreLoadFcn parameter, in the Simulink toolstrip, on the Modeling tab, in the Setup section, click Model Settings > Model Properties. In the Model Properties dialog box, on the Callbacks tab, select PreLoadFcn and enter this code.mySig = Simulink.Signal; mySig.Dimensions = [Inf 2]; mySig.DimensionsMode = 'Variable';Create a
Simulink.Signalobject namedmySigOutthat represents the output signal. Follow the same steps to load it into the model using the PreLoadFcn parameter.mySigOut = Simulink.Signal; mySigOut.Dimensions = [Inf 2]; mySigOut.DimensionsMode = 'Variable';Since the
DataTypeandComplexityvalues are not explicitly specified for these signal objects, the software assignsdoubleandrealto those attributes, respectively, during signal propagation.In the Function Caller block dialog box, set the Input argument specifications and Output argument specifications parameters to
mySigandmySigOut, respectively.Configure the Inport block connected to the Function Caller block input argument port so that the block accepts unbounded variable-size signals. In the Inport block dialog box:
Set the Signal Attributes > Variable-size signal parameter to
Yes.Specify a discrete sample time in the Execution > Sample time field. In this example, the sample time is 1s.
Clear the Interpolate data parameter on the Execution tab.
For the Argument Inport block within the Simulink Function block that calls the function defined inside the Function Caller block, set the Signal Attributes > Port dimensions parameter to match the dimensions of
mySig, which are[Inf 2].Similarly, in the Argument Outport block, set the Signal Attributes > Port dimensions parameter to match the dimensions of
mySigOut, which are[Inf 2]. To check whether the blocks you want to use within the Simulink Function block support unbounded variable-size signals, refer to the list of blocks in Supported Blocks and Features for Simulation.