Documentation

Type Function Arguments

About Function Arguments

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

By default, the data type mode forMATLAB Functionblock function arguments isInherited. 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.

Specifying Argument Types

To specify the type of aMATLAB Functionblock function argument:

  1. From the MATLAB Function Block Editor, selectEdit Datato open the Ports and Data Manager.

  2. In the left pane, select the argument of interest.

  3. In theDataproperties dialog box (right pane), click the Show data type assistant buttonto display the Data Type Assistant. Then, choose an option from theMode下拉菜单。

    TheDataproperties dialog box 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:

    Mode What 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 theMATLAB Functionblock function argument:
    • If scope isInput, data type is inherited from the input signal on the designated port.

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

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

    SeeInheriting Argument Data Types.

    Built in Select from the drop-down list of supported data types, as described inBuilt-In Data Types for Arguments.
    Fixed point

    Specify the fixed-point data properties as described inSpecifying Fixed-Point Designer Data Properties.

    Expression Enter an expression that evaluates to a data type, as described inSpecifying Argument Types with Expressions.
    Bus Object In theBus objectfield, enter the name of aSimulink.Busobject to define the properties of a MATLAB structure. You must define the bus object in the base workspace. SeeHow Structure Inputs and Outputs Interface with Bus Signals.

    Note

    You can click theEditbutton to create or modifySimulink.Busobjects using the Simulink Bus Editor (seeAttach Bus Signals to MATLAB Function Blocks.

    Enumerated In the Enumerated field, enter the name of aSimulink.IntEnumTypeobject that you define in the base workspace. SeeCode Generation for Enumerations.

Inheriting Argument Data Types

MATLAB Functionblock function arguments can inherit their data types, including fixed point types, from the signals to which they are connected.

  1. Select the argument of interest in the Ports and Data Manager

  2. In theDataproperties dialog, selectInherit: Same as Simulinkfrom theType下拉菜单。

SeeBuilt-In Data Types for Argumentsfor a list of supported data types.

Note

An argument can also inherit its complexity (whether its value is a real or complex number) from the signal that is connected to it. To inherit complexity, set theComplexityfield on theDataproperties dialog toInherited.

After you build the model, theCompiled Typecolumn of the Ports and Data Manager gives the actual type inherited from Simulink in the compiled simulation application.

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

Note

图书馆MATLAB Functionblocks can have inherited data types, sizes, and complexities like ordinaryMATLAB Functionblocks. However, all instances of the library block in a given model must have inputs with the same properties.

Built-In Data Types for Arguments

When you selectBuilt-inforData type mode,Dataproperties dialog displays aData typefield that provides a drop-down list of supported data types. You can also choose a data type from theData Typecolumn in the Ports and Data Manager. The supported data types are:

Data Type Description

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 ofMATLAB Functionblock function arguments as expressions in the Ports and Data Manager.

  1. Selectfrom theTypedrop-down menu of the Data properties dialog.

  2. In theTypefield, replace “” with an expression that evaluates to a data type. The following expressions are allowed:

    • Alias type from the MATLAB workspace, as described inSimulink.AliasType.

    • fixdtfunction to create aSimulink.NumericType对象描述一个定点或浮点data type

    • type(Stateflow)运营商基础type on previously defined data

SpecifyingFixed-Point DesignerData Properties

MATLAB Functionblocks can represent signals and parameter values as fixed-point numbers. To simulate models that use fixed-point data inMATLAB Functionblocks, you must install the Fixed-Point Designer™ product on your system.

You can set the following fixed-point properties:

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

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 Mode Description
Binary point(default)

If you select this mode, the Data Type Assistant displays theFraction Lengthfield, 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 theSlopeandBias.

  • Slope can be anypositivereal 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.

Note

You should use binary-point scaling whenever possible to simplify the implementation of fixed-point data in generated code. Operations with fixed-point data using binary-point scaling are performed with simple bit shifts and eliminate the expensive code implementations required for separate slope and bias values.

Data type override.Specify whether the data type override setting isInherit(default) orOff.

Calculate Best-Precision Scaling.仿真软件万博1manbetx的软件可以automatically calculate “best-precision” values for bothBinary pointandSlope and biasscaling, based on the Limit range properties you specify.

To automatically calculate best precision scaling values:

  1. SpecifyMinimum,Maximum, or both Limit range properties.

  2. ClickCalculate Best-Precision Scaling.

The Simulink software calculates the scaling values, then displays them in either theFraction Length, orSlopeandBiasfields.

Note

The Limit range properties do not apply toConstantorParameterscopes. Therefore, Simulink cannot calculate best-precision scaling for these scopes.

Fixed-point Details.You can view the following Fixed-point details:

Fixed-point Detail Description
Representable maximum The maximum number that can be represented by the chosen data type, sign, word length and fraction length (or data type, sign, slope and bias).
Maximum The maximum value specified.
Minimum The minimum value specified.
Representable minimum The minimum number that can be represented by the chosen data type, sign, word length and fraction length (or data type, sign, slope and bias).
Precision The precision for the given word length and fraction length (or slope and bias).

Using Data Type Override with the MATLAB Function Block

If you set the Data Type Override mode toDoubleorSinglein Simulink, theMATLAB Functionblock sets the type of all inherited input signals and parameters tofi doubleorfi singleobjects respectively (seeMATLAB Function Block with Data Type Override(Fixed-Point Designer) for more information). You must check the data types of your inherited input signals and parameters and use the Ports and Data Manager (seePorts and Data Manager) to set explicit types for any inputs that should not be fixed-point. Some operations, such assin, are not applicable to fixed-point objects.

Note

If you do not set the correct input types explicitly, you may encounter compilation problems after setting Data Type Override.

How Do I Set Data Type Override?

Related Examples

More About

Was this topic helpful?