MATLAB Function blocks enable you to define custom functionality in Simulink® models by using the MATLAB® language. They are the easiest way to bring MATLAB code into Simulink. MATLAB Function blocks support C/C++ code generation from Simulink Coder™ and Embedded Coder®.
Use these blocks specifically when:
You have an existing MATLAB function that models custom functionality, or it would be easy for you to create such a function.
Your model requires custom functionality that is not or cannot be captured in the Simulink graphical language.
You find it easier to model custom functionality by using a MATLAB function than by using a Simulink block diagram.
The custom functionality that you want to model does not include continuous or discrete dynamic states. To model dynamic states, use S-functions. See Create and Configure MATLAB S-Functions.
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.
Because the MATLAB Function block relies on code generation technology, it shares much in common with MATLAB Coder. C/C++ code generation limitations for MATLAB Coder also apply to MATLAB Function blocks. However, the MATLAB Function block is self-contained within Simulink, and does not require MATLAB Coder. To generate standalone C/C++ code from a model that contains MATLAB Function blocks, use Simulink Coder.
The following describes what you can use a MATLAB Function for in your model. To see how to use a MATLAB Function block in an example, see Create Custom Functionality Using MATLAB Function Block.
By default, both the size and type of input and output signals to a MATLAB Function block are inherited from the corresponding Simulink signals. You can also choose to specify the size and type of inputs and outputs explicitly in the Ports and Data Manager or in the Model Explorer. See Ports and Data Manager and Model Explorer.
MATLAB Function blocks are supported for C/C++ code generation with Simulink Coder and Embedded Coder. By using code generation on a Simulink model that contains a MATLAB Function block, you can deploy MATLAB functionality outside the MATLAB environment.
For more information on C/C++ code generation from a Simulink model, see Simulink Coder.
In a MATLAB Function block, you can only use the subset of the MATLAB language and language features that are supported for C/C++ code generation. For a list of functions supported for code generation, see Functions and Objects Supported for C/C++ Code Generation. For supported language features, see MATLAB Language Features Supported for C/C++ Code Generation.
For simulation, you can call extrinsic functions from a MATLAB Function block. Extrinsic functions are functions that are not supported for C/C++ code generation but which can be dispatched to the MATLAB environment for execution during run time. Extrinsic functions execute in the workspace during model simulation.
For code generation, Simulink Coder attempts to compile all functions in a MATLAB Function block unless you explicitly declare them as extrinsic. Extrinsic function calls are omitted from generated standalone code, such as standalone C/C++ source code or executable files. See Resolution of Function Calls for Code Generation and Using the coder.extrinsic Construct.
From MATLAB Function blocks, you can call functions defined in a Simulink Function block. You can call Stateflow® functions when you select the Export Chart Level Functions (Make Global) and Allow exported functions to be called by Simulink check boxes in the chart Properties dialog box. To learn more about how to call functions defined in Simulink Function and Stateflow blocks, see Simulink functions: Simulink Function block, exported Stateflow graphical and MATLAB functions.