| Contents | Index |
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 Using the MATLAB Function Block in the Simulink documentation for a comprehensive overview.
You write the MATLAB function in the following editor:

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:

The MATLAB Function block can generate efficient embeddable code. To generate embeddable code, the MATLAB Function block relies on an analysis that determines the size, class, and complexity of each variable. This analysis imposes the following restrictions:
The first assignment to a variable defines its, size, class, and complexity.
See Defining MATLAB Variables for C/C++ Code Generation in the Code Generation from MATLAB documentation.
Generally, you cannot reassign variable properties after the initial assignment except when using variable-size data or reusing variables in the code for different purposes.
See When You Can Reassign Variable Properties for C/C++ Code Generation in the Code Generation from MATLAB documentation.
In addition to language restrictions, the MATLAB Function block supports only a subset of the functions available in MATLAB. A list of supported functions is given in Functions Supported for Code Generation in the Code Generation from MATLAB documentation. These functions include functions in common categories, such as:
Arithmetic functions like plus, minus, and power
See Functions Supported for Code Generation — Categorical List in the Code Generation from MATLAB documentation 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. |
To support visualization of data, the MATLAB Function block supports calls to MATLAB functions for simulation only. See Calling MATLAB Functions in the Code Generation from MATLAB documentation 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.
In the Model Explorer, 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 using the Model Explorer. See Typing Function Arguments, Sizing Function Arguments, and Parameter Arguments in MATLAB Function Block Functions for more detailed descriptions of variables that you use in MATLAB Function blocks.
Recursive calls are not allowed in MATLAB Function blocks.
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 in the Simulink documentation.
For more information on fixed-point support for this block, refer to Code Acceleration and Code Generation from MATLAB for Fixed-Point Algorithms in the Fixed-Point Toolbox documentation.
The MATLAB Function block supports Simulink frames. For more information, see Sample- and Frame-Based Concepts in the DSP System Toolbox documentation.
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
Direct Feedthrough | Yes |
Sample Time | Specified in the Sample time parameter |
Scalar Expansion | Yes |
Dimensionalized | Yes |
Multidimensionalized | Yes |
Zero-Crossing Detection | No |

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 |