MATLAB Function

Include MATLAB code in models that generate embeddable C code


User-Defined Functions


With a MATLAB Function block, you can write a MATLAB® function for use in a Simulink® model. The MATLAB function you create executes for simulation and generates code for a Simulink Coder™ target. If you are new to the Simulink and MATLAB products, see What Is a MATLAB Function Block? and Create Model That Uses MATLAB Function Block for an overview.

Double-clicking the MATLAB Function block opens its editor, where you write the MATLAB function, as in this example:

To learn more about this editor, see MATLAB Function Block Editor.

You specify input and output data to the MATLAB Function block in the function header as arguments and return values. The argument and return values of the preceding example function correspond to the inputs and outputs of the block in the model:

You can also define data, input triggers, and function call outputs using the Ports and Data Manager, which you access from the MATLAB Function Block Editor by selecting Edit Data. See Ports and Data Manager.

The MATLAB Function block generates efficient embeddable code based on an analysis that determines the size, class, and complexity of each variable. This analysis imposes the following restrictions:

In addition to language restrictions, the MATLAB Function block supports a subset of the functions available in MATLAB. A list of supported functions is given in Functions and Objects Supported for C and C++ Code Generation — Alphabetical List. These functions include functions in common categories, such as:

See Functions and Objects Supported for C and C++ Code Generation — Category List for a complete list of function categories.

    Note   Although the code for this block attempts to produce exactly the same results as MATLAB, differences might occur due to rounding errors. These numerical differences, which might be a few eps initially, can magnify after repeated operations. Reliance on the behavior of nan is not recommended. Different C compilers can yield different results for the same computation.

    Note:   In the MATLAB Function block, the %#codegen directive is included to emphasize that the block's MATLAB algorithm is always intended for code generation. The %#codegen directive, or the absence of it, does not change the error checking behavior in the context of the MATLAB Function block. For more information see Compilation Directive %#codegen.

To support visualization of data, the MATLAB Function block supports calls to MATLAB functions for simulation only. See Call MATLAB Functions to understand some of the limitations of this capability, and how it integrates with code analysis for this block. If these function calls do not directly affect any of the Simulink inputs or outputs, the calls do not appear in Simulink Coder generated code.

From MATLAB Function blocks, you can also call functions defined in a Simulink Function block. You can call Stateflow® functions with Export Chart Level Functions (Make Global) and Allow exported functions to be called by Simulink checked in the chart Properties dialog box.

In the Ports and Data Manager, you can declare a block input to be a Simulink parameter instead of a port. The MATLAB Function block also supports inheritance of types and size for inputs, outputs, and parameters. You can also specify these properties explicitly. See Type Function Arguments, Size Function Arguments, and Add Parameter Arguments for descriptions of variables that you use in MATLAB Function blocks.

Recursive calls are not allowed in MATLAB Function blocks.

By default, MATLAB Function blocks have direct feedthrough enabled. To disable it, in the Ports and Data Manager, clear the Allow direct feedthrough check box. Nondirect feedthough enables semantics to ensure that outputs rely only on current state. Using nondirect feedthrough enables you to use MATLAB Function blocks in a feedback loop and prevent algebraic loops.

Data Type Support

The MATLAB Function block accepts inputs of any type that Simulink supports, including fixed-point and enumerated types. For more information, see Data Types Supported by Simulink.

Data types supported by MATLAB but not supported by Simulink may not be passed between the Simulink model and the function within the MATLAB Function block. These types may be used within the MATLAB Function block.

For more information on fixed-point support for this block, refer to Fixed-Point Data Types with MATLAB Function Block and MATLAB Function Block with Data Type Override.

Parameters and Dialog Box

The block dialog box for a MATLAB Function block is identical to the dialog box for a Subsystem block. See the reference page for the Subsystem, Atomic Subsystem, Nonvirtual Subsystem, CodeReuse Subsystem blocks for information about each block parameter.


The following models shows how to use the MATLAB Function block:

  • sldemo_radar_eml

  • sldemo_eml_galaxy


Data Types

Double | Single | Boolean | Base Integer | Fixed-Point | Enumerated | Bus

Sample Time

Specified in the Sample time parameter

Direct Feedthrough

Yes (default). To disable, in the Ports and Data Manager, clear the Allow direct feedthrough check box.

Multidimensional Signals


Variable-Size Signals


Zero-Crossing Detection


Code Generation


Introduced in R2011a

Was this topic helpful?