Define and Modify Variable Data Types
When you create variables in a MATLAB Function block, you can use the Type property to set the data type. Variables can inherit their data types, or be set to builtin, fixedpoint, or enumerated data types. Variables can also be nonvirtual buses. By default, MATLAB Function block variables inherit their data type.
For more information on creating variables, see Create and Define MATLAB Function Block Variables.
Specify Variable Data Types
You can specify the data types by using the Symbols pane and Property Inspector, or the Model Explorer.
To specify the data type using the Symbols pane and Property Inspector:
Doubleclick the MATLAB Function block to open the MATLAB Function Block Editor.
In the Function tab, click Edit Data.
In the Symbols pane, select the variable.
In the Property Inspector, in the Properties tab, select the data type from the Type property.
To specify the data type of a variable by using the Model Explorer:
Open the Model Explorer. In the Modeling tab, in the Design section, click Model Explorer.
In the Model Hierarchy pane select the MATLAB Function block.
Click the variable you want to modify.
Select the data type from the Type property.
In the Model Explorer, you can also filter the data type options. In the General tab, click the Show data type assistant button to display the Data Type Assistant parameters. Then, choose an option from the Mode parameter. Based on the mode you select, specify the data type:
Mode  What to Specify 

Inherit (default)  The inherited data depends on the Scope property:

Built in  Select from a list of builtin data types. 
Fixed point  Specify the fixedpoint data properties. 
Enumerated  Enter the name of a Simulink.IntEnumType object that you
define in the base workspace. See Code Generation for Enumerations. 
Bus Object  In the Bus object field, enter the name of a
Note You can click the Edit
button to create or modify

Expression  Enter an expression that evaluates to a data type. 
Inheriting Data Types
MATLAB Function block variables can inherit their data types, including
fixed point types, from their connected signals. To make a variable inherit a data type, set
the Type property to Inherit: Same as
Simulink
.
An argument can also inherit complexity from the signal connected to it. To inherit complexity, set the Complexity property to Inherited.
After you build the model, the CompiledType column of the Model Explorer gives the actual type inherited from Simulink. If the expected type matches the inferred type, inheritance is successful.
BuiltIn Data Types
In the Model Explorer, when you expand the Data Type Assistant and set
Mode to Built in
, you can set
Type to these builtin data types. The builtin data types
are:
Data Type  Description 

 64bit doubleprecision floating point 
 32bit singleprecision floating point 
 A halfprecision data type occupies 16 bits of memory, but its floatingpoint representation enables it to handle wider dynamic ranges than integer or fixedpoint data types of the same size. See The HalfPrecision Data Type in Simulink (FixedPoint Designer). 
 64bit signed integer 
 32bit signed integer 
 16bit signed integer 
 8bit signed integer 
 64bit unsigned integer 
 32bit unsigned integer 
 16bit unsigned integer 
 8bit unsigned integer 
 Boolean 
 String scalar 
FixedPoint Designer Data Type Properties
To represent variables as fixedpoint numbers in MATLAB Function blocks, you must install FixedPoint Designer™.
You can set the following fixedpoint properties:
Signedness
Select whether you want the fixedpoint variable to be
Signed
or Unsigned
. Signed
variables can represent positive and negative quantities. Unsigned variables 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 fixedpoint variable to avoid overflow conditions and minimize quantization issues. You can select these scaling modes:
Scaling Mode  Description 

Binary point (default)  The Data Type Assistant displays the Fraction Length parameter, which specifies 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 
Slope and bias  The Data Type Assistant displays the Slope and Bias parameters:
You can enter slope and bias as expressions that contain parameters defined in the MATLAB workspace. 
Note
Use binarypoint scaling whenever possible to simplify the implementation of fixedpoint numbers in generated code. Operations with fixedpoint numbers that use binarypoint 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 is Inherit
or
Off
. For more information, see Data Type Override with MATLAB Function Block (FixedPoint Designer).
Calculate BestPrecision Scaling
Have Simulink automatically calculate bestprecision values for both Binary
point
and Slope and bias
scaling, based on the
Minimum and Maximum properties you
specify.
To automatically calculate best precision scaling values:
Specify the Minimum or Maximum properties.
Click Calculate BestPrecision Scaling.
Simulink calculates the scaling values, then displays them in either the Fraction length, or the Slope and Bias fields.
Note
The Minimum and Maximum properties do not
apply to variables with the Scope property set to
Constant
or Parameter
. The
software cannot calculate bestprecision scaling for these kinds of variables.
Fixedpoint details
Displays information about the fixedpoint variable that is defined in the Data Type Assistant:
Minimum
andMaximum
show the same values that you specify in the Minimum and Maximum properties.Representable minimum
,Representable maximum
, andPrecision
show the minimum value, maximum value, and precision that the fixedpoint variable can represent.
If the value of a field cannot be determined without first compiling the model, the
Fixedpoint details subpane shows the value as
Unknown
. The values displayed by the Fixedpoint
details subpane do not automatically update if you change the values that
define the fixedpoint variable. To update the values shown in the Fixedpoint
details subpane, click Refresh Details.
Clicking Refresh Details does not modify the variable. It changes only the display. To apply the displayed values, click Apply or OK.
The Fixedpoint details subpane indicates issues resulting from the fixedpoint variable specification. For example, this figure shows two issues.
The row labeled Maximum
indicates that the value specified by the
Maximum property is not representable by the
fixedpoint variable. To correct the issue, make one of these modifications so the
fixedpoint data type can represent the maximum value:
Decrease the value in the Maximum property.
Increase Word length.
Decrease Fraction length.
The row labeled Minimum
shows the message Cannot
evaluate
because evaluating the expression MySymbol
,
specified by the Minimum property, does not return a numeric value.
When an expression does not evaluate successfully, the Fixedpoint
details subpane shows the unevaluated expression (truncating to 10 characters
as needed) in place of the unavailable value. To correct this issue, define
MySymbol
in the base workspace to provide a numeric value.
If you click Refresh Details, the issue indicator and
description are removed and the value of MySymbol
appears in place of
the unevaluated text.
Specify Data Types with Expressions
You can specify the types of MATLAB Function block variables as expressions by using the Model Explorer or the Property Inspector.
To use the Model Explorer, set the Mode property to
Expression
. In the Type property, replace
<data type expression>
with an expression that evaluates
to a data type.
To use the Property Inspector, doubleclick the Type property, clear the contents, and enter an expression.
You can use the following expressions:
Alias type from the MATLAB workspace, as described in
Simulink.AliasType
.fixdt
function to create aSimulink.NumericType
object describing a fixedpoint or floatingpoint data type.type
(Stateflow) operator, to base the type on previously defined data.
For example, suppose you want to designate the workspace variable
myDataType
as an alias for a single
data type to use
as an expression in the Type property of an MATLAB
Function block input variable. Create an instance of the
Simulink.AliasType
class and set its BaseType
property
by entering these commands:
myDataType = Simulink.AliasType;
myDataType.BaseType = "single";
In the Property Inspector, enter the data type alias name,
myDataType
, as the value in the Type
property.
Note
MATLAB Function blocks do not support code generation if one of the variables uses an alias type and is variable size. This limitation does not apply to input or output variables. For more information on defining variablesize variables and generating code with them, see Declare VariableSize MATLAB Function Block Variables and Code Generation for VariableSize Arrays.
Programmatically Change Variable Data Types
You can change MATLAB Function block variable data types programmatically
by retrieving the Stateflow.EMChart
object of the
block. For more information, see Configure MATLAB Function Blocks Programmatically.