| Contents | Index |
| On this page… |
|---|
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 the MATLAB software than in the graphical language of the Simulink product. From the MATLAB Function block, you can generate readable, efficient, and compact C/C++ code for deployment to desktop and embedded applications. For more information, see About Code Generation from MATLAB Algorithms in the code generation from MATLAB documentation. For more information on fixed-point support in MATLAB, refer to Code Acceleration and Code Generation from MATLAB for Fixed-Point Algorithms in the Fixed-Point Toolbox™ documentation.
Here is an example of a Simulink model that contains a MATLAB Function block:

The MATLAB Function block contains the following algorithm:
function [mean, stdev] = stats(vals)
% Calculates a statistical mean and a standard deviation
% for the values in vals
coder.extrinsic('plot');
len = length(vals);
mean = avg(vals,len);
stdev = sqrt(sum(((vals-avg(vals,len)).^2))/len);
plot(vals,'-+');
function mean = avg(array,size)
mean = sum(array)/size;You will build this model in Creating an Example Model That Uses a MATLAB Function Block.
Defining Local Variables for Code Generation. If you intend to generate code from the MATLAB algorithm in a MATLAB Function block, you must explicitly assign the class, size, and complexity of local variables before using them in operations or returning them as outputs (see Preparing MATLAB Code for C/C++ and MEX Code Generation and Why Define Variables Differently for Code Generation? in the Code Generation from MATLAB documentation. In the example function stats, the local variable len is defined before being used to calculate mean and standard deviation:
len = length(vals);
Generally, once you assign properties to a variable, you cannot redefine its class, size, or complexity elsewhere in the function body, but there are exceptions (see When You Can Reassign Variable Properties for C/C++ Code Generation.
Declaring Extrinsic Functions. If your MATLAB algorithm calls functions that are not supported for code generation, you must declare them to be extrinsic by using the coder.extrinsic construct. This declaration ensures that the functions are not compiled, but instead dispatched it to MATLAB for execution during simulation of the model. The example function stats declares the plot function to be extrinsic:
coder.extrinsic('plot');Here, plot is used to visualize the results during simulation, but there is no need to generate code for the function.
See Calling Functions for Code Generation in the Code Generation from MATLAB documentation for more information.
MATLAB Function blocks can call any of the following types of functions:
Subfunctions are defined in the body of the MATLAB Function block. In the preceding example, avg is a subfunction. See Calling Subfunctions in the Code Generation from MATLAB documentation.
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 Calling Supported Toolbox Functions in the Code Generation from MATLAB documentation. For a complete list of supported functions, see Functions Supported for Code Generation.
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 Calling MATLAB Functions in the Code Generation from MATLAB documentation.
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 (see Functions Supported for Code Generation in the Code Generation from MATLAB documentation). 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 Model Explorer or Ports and Data Manager (see Ports and Data Manager).
![]() | Using the MATLAB Function Block | Creating an Example Model That Uses a MATLAB Function Block | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |