MATLAB Function
R2026bInclude MATLAB code in Simulink models
Libraries:
Simulink
HDL Coder /
User-Defined Functions
Description
The MATLAB Function block enables you to write MATLAB® functions that execute in Simulink® models. The MATLAB function executes during simulation at each time step. For more information about integrating MATLAB code into your Simulink models, see Implement MATLAB Functions in Simulink with MATLAB Function Blocks. If you have a Simulink Coder™ license, you can also generate C/C++ code from a MATLAB Function block for a Simulink Coder target.
Double-click the MATLAB Function block to open the MATLAB Function Block Editor, where you write the MATLAB function. You can also define variables, add an input trigger, and create function call outputs by using the Model Explorer or the Symbols pane. For more information, see Create MATLAB Function Block Variables, Manage the Input Trigger of a MATLAB Function Block, and Manage Function Call Outputs of a MATLAB Function Block.
In the Symbols pane, 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 specify these properties explicitly. See Specify Data Types of Variables in MATLAB Function Blocks, Specify Size of MATLAB Function Block Variables, and Access Workspace Data in MATLAB Function Blocks by Using Parameter Variables.
When you simulate a model that contains a MATLAB Function block, the software generates binary code or C/C++ MATLAB executable (MEX) code from the block and integrates this code with the model. The MATLAB Function block uses the same infrastructure as MATLAB Coder, which you use to generate C/C++ code from MATLAB code outside of Simulink. Consequently, the MATLAB Function block code must use MATLAB conventions that are supported for code generation. See Code and Integration Limitations for MATLAB Function Blocks.
To support visualization of data, the MATLAB Function block supports calls to MATLAB functions for simulation only. See Using Extrinsic Functions (MATLAB Coder). If you generate code using Simulink Coder, the calls do not appear in the generated code if the function calls do not directly affect the Simulink inputs or outputs.
Examples
Extended Examples
Limitations
Although the code for this block attempts to produce exactly the same results as MATLAB, differences might occur due to rounding errors. These numeric differences, which might be a few
epsinitially, can magnify after repeated operations. Reliance on the behavior ofnanis not recommended. Different C compilers can yield different results for the same computation.Recursive calls are not allowed in MATLAB Function blocks.
Ports
Input
Output
Parameters
Block Characteristics
Programmatic Use: Add Block
To add the block programmatically, use the add_block function with one of these library block paths as the source.
"simulink/User-Defined Functions/MATLAB Function"
"hdlsllib/User-Defined Functions/MATLAB Function"
Example: add_block("simulink/User-Defined Functions/MATLAB Function","myModel/myBlock",MakeNameUnique="on")
For information about programmatic access to individual block parameters, see the Programmatic Use section of parameter descriptions on this page.
Tips
You can call functions defined in:
Simulink Function blocks.
Stateflow® functions in charts. To call functions in charts, enable the Export Chart Level Functions and Treat exported functions as globally visible properties.
By default, MATLAB Function blocks do not include the
%#codegen(MATLAB Coder) directive, but check for errors as if it is included. Adding the%#codegendirective to a MATLAB Function block does not affect error checking.


