Implementing MATLAB Functions Using Blocks

The MATLAB Function block is the easiest way to implement custom MATLAB® functions into Simulink. To model the functionality that is not captured by the Simulink® graphical language, or for algorithms that are easily represented in the MATLAB textual language, you can use MATLAB Function blocks. Use MATLAB Function blocks to generate readable, efficient, and compact C/C++ code for deployment to desktop and embedded applications.

In other words, MATLAB Function blocks enable you to define custom functionality using the MATLABlanguage. These blocks are a good starting point to create custom functionality if:

  • You have an existing MATLAB function that models the custom functionality.

  • You find it easier to model custom functionality using a MATLAB function than by using a Simulink block diagram.

  • The custom functionality does not include continuous or discrete dynamic states. To model dynamic states, use S-functions.

Decide When to Use MATLAB Function Blocks

MATLAB Function blocks provide the following capabilities:

  • Allow you to build MATLAB functions into embeddable applications — MATLAB Function blocks support a subset of MATLAB toolbox functions that generate efficient C/C++ code. For more information, see Functions and Objects Supported for C/C++ Code Generation — Alphabetical List. With this support, you can use Simulink Coder™ to generate embeddable C code from MATLAB Function blocks that implement a variety of sophisticated mathematical applications. In this way, you can build executables that harness MATLAB functionality but run outside the MATLAB environment.

  • Inherit properties from Simulink input and output signals — By default, both the size and type of input and output signals to a MATLAB Function block are inherited from Simulink signals. You can also choose to specify the size and type of inputs and outputs explicitly in the Ports and Data Manager (see Ports and Data Manager) or in the Model Explorer (see Search and Edit Using Model Explorer).

Call Functions from MATLAB Function Blocks

To integrate MATLAB code into Simulink models, you can call any of these functions from a MATLAB Function block.

  • Local functions — You can call local functions defined in the body of the MATLAB Function block.

  • MATLAB toolbox functions that support code generation — You can call toolbox functions that support code generation from MATLAB Function blocks. When you build your model with Simulink Coder, these functions generate C code that is optimized to meet the memory and performance requirements of desktop and embedded environments. For a list of supported functions, see Functions and Objects Supported for C/C++ Code Generation — Alphabetical List.

  • MATLAB functions that do not support code generation — You can also call extrinsic functions from MATLAB Function blocks. These are functions on the MATLAB path that the compiler dispatches to MATLAB for execution because the target language does not support them. They do not generate code but execute only in the MATLAB workspace during model simulation. The Simulink Coder software attempts to compile all MATLAB functions unless you explicitly declare them to be extrinsic by using coder.extrinsic. See Declaring MATLAB Functions as Extrinsic Functions.

    The code generation software detects calls to many common visualization functions, such as plot, disp, and figure. For MEX code generation, it automatically calls out to MATLAB for these functions. For standalone code generation, it does not generate code for these visualization functions. Therefore, you do not have to declare these functions extrinsic using the coder.extrinsic function. See Resolution of Function Calls for Code Generation.

  • Functions from Simulink Function blocks and Stateflow® blocks — From MATLAB Function blocks, you can also call functions defined in a Simulink Function block. You can call Stateflow functions when you select the Export Chart Level Functions (Make Global) and Allow exported functions to be called by Simulink check boxes in the chart Properties dialog box. To learn more about how to call functions defined in Simulink Function and Stateflow blocks, see Using Simulink Function Blocks and Exported Stateflow Functions.

Use Nondirect Feedthrough in a MATLAB Function Block

In Simulink blocks, direct feedthrough means that the output of a block is controlled directly by the value of an input port signal. In nondirect feedthrough, the value of the output signal does not depend on the value of the input signal in at least one function during the simulation.

By default, MATLAB Function blocks have direct feedthrough enabled. If you disable direct feedthrough, the Simulink semantics ensures 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.

To use nondirect feedthrough, in the Ports and Data Manager, in the MATLAB Function Block Editor, select Edit Data on the Editor tab and clear the Allow direct feedthrough check box. For more information, see Ports and Data Manager.


Do not program outputs to rely on inputs or updated persistent variables. For example, do not use this code in a nondirect feedthrough block:

counter = counter + 1;  	% update state
output = counter;       	% compute output based on updated state
Instead, use this code:
output = counter;       	% compute output based on current state
counter = counter + 1;  	% update state


To use nondirect feedthrough, you need to enable the function inlining using coder.inline.

See Also

Related Topics