What Is a MATLAB Function Block?

The MATLAB Function block allows you to add MATLAB® functions to Simulink® models for deployment to desktop and embedded processors. This capability is useful for coding algorithms that are better stated in the textual language of MATLAB than in the graphical language of Simulink. From the MATLAB Function block, you can generate readable, efficient, and compact C/C++ code for deployment to desktop and embedded applications.

Calling Functions in MATLAB Function Blocks

MATLAB Function blocks can call any of the following types of functions:

  • Local functions

    Local functions are defined in the body of the MATLAB Function block. In the preceding example, avg is a local function. See Call Local Functions.

  • MATLAB toolbox functions that support code generation

    From MATLAB Function blocks, you can call toolbox functions that support code generation. 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. In the preceding example, length, sqrt, and sum are examples of toolbox functions that support code generation. See Call Supported Toolbox Functions. For a complete list of supported functions, see Functions and Objects Supported for C and C++ Code Generation — Alphabetical List.

  • MATLAB functions that do not support code generation

    From MATLAB Function blocks, you can also call extrinsic functions. These are functions on the MATLAB path that the compiler dispatches to MATLAB software for execution because the target language does not support them. These functions do not generate code; they execute only in the MATLAB workspace during simulation of the model. 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. This capability removes the requirement to declare these functions extrinsic using the coder.extrinsic function.

    See Resolution of Function Calls for Code Generation.

Was this topic helpful?