Skip to Main Content Skip to Search
Product Documentation

Introduction to MATLAB Function Blocks

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 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.

Example: Calculating Statistical Mean and Standard Deviation

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.

Calling Functions in MATLAB Function Blocks

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

Why Use MATLAB Function Blocks?

MATLAB Function blocks provide the following capabilities:

  


Related Products & Applications

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