Skip to Main Content Skip to Search
Product Documentation

Typing Function Arguments

About Function Arguments

You create function arguments for a MATLAB Function block by entering them in its function header in the MATLAB Function Block Editor. When you define arguments, the Simulink software creates corresponding ports on the MATLAB Function block that you can attach to signals. You can select a data type mode for each argument that you define for a MATLAB Function block. Each data type mode presents its own set of options for selecting a data type.

By default, the data type mode for MATLAB Function block function arguments is Inherited. This means that the function argument inherits its data type from the incoming or outgoing signal. To override the default type, you first choose a data type mode and then select a data type based on the mode. The following procedure describes how to use the Model Explorer to set data types for function arguments. You can also use the Ports and Data Manager tool (see Ports and Data Manager).

Specifying Argument Types

To specify the type of a MATLAB Function block function argument:

  1. From the MATLAB Function Block Editor, select Tools > Model Explorer.

    Model Explorer appears with the MATLAB Function block highlighted in the Model Hierarchy pane.

  2. In the Contents pane (in the middle), click the row containing the argument of interest.

  3. In the Data properties dialog (on the right), click the Show data type assistant button to display the Data Type Assistant. Then, choose an option from the Mode drop-down menu, as shown:

    The Data properties dialog changes dynamically to display additional fields for specifying the data type associated with the mode.

  4. Based on the mode you select, specify a desired data type:

    ModeWhat to Specify
    Inherit (default)You cannot specify a value. The data type is inherited from previously-defined data, based on the scope you selected for the MATLAB Function block function argument:
    • If scope is Input, data type is inherited from the input signal on the designated port.

    • If scope is Output, data type is inherited from the output signal on the designated port.

    • If scope is Parameter, data type is inherited from the associated parameter, which can be defined in the Simulink masked subsystem or the MATLAB workspace.

    See Inheriting Argument Data Types.

    Built inIn the Data type field, select from the drop-down list of supported data types, as described in Built-In Data Types for Arguments.
    Fixed point

    Specify the fixed-point data properties as described in Specifying Simulink Fixed Point Data Properties.

    ExpressionEnter an expression that evaluates to a data type, as described in Specifying Argument Types with Expressions.
    Bus ObjectIn the Bus object field, enter the name of a Simulink.Bus object to define the properties of a MATLAB structure. You must define the bus object in the base workspace. See Working with Structures and Bus Signals.

      Note   You can click the Edit button to create or modify Simulink.Bus objects using the Simulink Bus Editor (see Using the Bus Editor in the Simulink User's Guide).

Inheriting Argument Data Types

MATLAB Function block function arguments can inherit their data types, including fixed point types, from the signals to which they are connected. Select the argument of interest in the Contents pane of the Model Explorer or Ports and Data Manager, and set data type mode using one of these methods:

See Built-In Data Types for Arguments for a list of supported data types.

Once you build the model, the Compiled Type column of the Model Explorer or Ports and Data Manager gives the actual type used in the compiled simulation application.

In the following figure, a MATLAB Function block argument inherits its data type from an input signal of type double:

The inherited type of output data is inferred from diagram actions that store values in the specified output. In the preceding example, the variables mean and stdev are computed from operations with double operands, which yield results of type double. If the expected type matches the inferred type, inheritance is successful. In all other cases, a mismatch occurs during build time.

Built-In Data Types for Arguments

When you select Built-in for Data type mode, the Data properties dialog displays a Data type field that provides a drop-down list of supported data types. You can also choose a data type from the Data Type column in the Contents pane of the Model Explorer or Ports and Data Manager. The supported data types are:

Data TypeDescription

double

64-bit double-precision floating point

single

32-bit single-precision floating point

int32

32-bit signed integer

int16

16-bit signed integer

int8

8-bit signed integer

uint32

32-bit unsigned integer

uint16

16-bit unsigned integer

uint8

8-bit unsigned integer

boolean

Boolean (1 = true; 0 = false)

Specifying Argument Types with Expressions

You can specify the types of MATLAB Function block function arguments as expressions in the Model Explorer or Ports and Data Manager. Follow these steps:

  1. Select <data type expression> from the Type drop-down menu of the Data properties dialog.

  2. In the Type field, replace "<data type expression>" with an expression that evaluates to a data type. The following expressions are allowed:

    • Alias type from the MATLAB workspace, as described in Creating a Data Type Alias in the Simulink reference documentation.

    • fixdt function to create a Simulink.NumericType object describing a fixed-point or floating-point data type

    • type operator, to base the type on previously defined data

      In the following figure, the data type of input argument data1 is int32. The data type of input argument data2 is based on data1 using the expression type(data1).

      When the model is compiled, the actual type of data2 appears in the Compiled Type column in the Contents pane:

Specifying Simulink Fixed Point Data Properties

MATLAB Function blocks can represent signals and parameter values as fixed-point numbers. To simulate models that use fixed-point data in MATLAB Function blocks, you must install the Simulink Fixed Point product on your system (see Product Overview in the Simulink Fixed Point documentation).

When you select the Fixed point data type Mode, the Data Type Assistant displays fields for additional information about your fixed-point data, as in this example:

You can set the following fixed-point properties:

Signedness.  Select whether you want the fixed-point data to be Signed or Unsigned. Signed data can represent positive and negative quantities. Unsigned data represents positive values only. The default is Signed.

Word length.  Specify the size (in bits) of the word that will hold the quantized integer. Large word sizes represent large quantities with greater precision than small word sizes. Word length can be any integer between 0 and 128 bits. The default is 16.

Scaling.  Specify the method for scaling your fixed point data to avoid overflow conditions and minimize quantization errors. You can select the following scaling modes:
Scaling ModeDescription
Binary point (default)

If you select this mode, the Data Type Assistant displays the Fraction Length field, specifying the binary point location.

Binary points can be positive or negative integers. A positive integer moves the binary point left of the rightmost bit by that amount. For example, an entry of 2 sets the binary point in front of the second bit from the right. A negative integer moves the binary point further right of the rightmost bit by that amount, as in this example:

The default is 0.

Slope and bias

If you select this mode, the Data Type Assistant displays fields for entering the Slope and Bias.

  • Slope can be any positive real number. The default is 1.0.

  • Bias can be any real number. The default value is 0.0.

You can enter slope and bias as expressions that contain parameters defined in the MATLAB workspace.

Data type override.  Specify whether the data type override setting is Inherit (default) or Off.

Calculate Best-Precision Scaling.  The Simulink software can automatically calculate "best-precision" values for both Binary point and Slope and bias scaling, based on the Limit range properties you specify on the Value Attributes tab.

To automatically calculate best precision scaling values:

  1. Select the Value Attributes tab.

  2. Specify Minimum, Maximum, or both Limit range properties.

  3. Select the General tab.

  4. Click Calculate Best-Precision Scaling.

The Simulink software calculates the scaling values, then displays them in either the Fraction Length, or Slope and Bias fields.

Fixed-point Details.  You can view the following Fixed-point details:
Fixed-point DetailDescription
Representable maximumThe maximum number that can be represented by the chosen data type, sign, word length and fraction length (or data type, sign, slope and bias).
MaximumThe maximum value specified on the Value Attributes tab.
MinimumThe minimum value specified on the Value Attributes tab.
Representable minimumThe minimum number that can be represented by the chosen data type, sign, word length and fraction length (or data type, sign, slope and bias).
PrecisionThe precision for the given word length and fraction length (or slope and bias).

Lock data type setting against changes by the fixed-point tools.  Specify whether you want to prevent replacement of the current data type with a type chosen by the Fixed-Point Tool or Fixed-Point Advisor. The default setting allows replacement. See Scaling in the Simulink Fixed Point documentation for instructions on autoscaling fixed-point data.

Using Data Type Override with the MATLAB Function Block

If you set the Data Type Override mode to Double or Single in Simulink, the MATLAB Function block sets the type of all inherited input signals and parameters to fi double or fi single objects respectively (see Using the MATLAB Function Block with Data Type Override in the Fixed-Point Toolbox User's Guide for more information). You must check the data types of your inherited input signals and parameters and use the Ports and Data Manager (see Ports and Data Manager) to set explicit types for any inputs that should not be fixed-point. Some operations, such as sin, are not applicable to fixed-point objects.

 How Do I Set Data Type Override?

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS