| Products & Services | Industries | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Stateflow |
| Contents | Index |
| Learn more about Stateflow |
You can use Embedded MATLAB functions to add MATLAB functions to a Stateflow chart. This capability is useful for coding algorithms that are better expressed in the textual MATLAB language than in the graphical Stateflow action language. Embedded MATLAB functions work with a subset of the MATLAB language called the Embedded MATLAB subset, which provides optimizations for generating efficient, production-quality C code for embedded applications. For more information, see Working with the Embedded MATLAB Subset.
This example shows a Simulink model with a Stateflow chart that contains an Embedded MATLAB function.

You will build a similar model in Building a Simulink Model with an Embedded MATLAB Function.
Note in this example that the Embedded MATLAB function can call any of these types of functions:
Subfunctions
Subfunctions are defined in the body of the Embedded MATLAB function. In the preceding example, avg is a subfunction. See Calling Subfunctions in the Embedded MATLAB User's Guide.
Embedded MATLAB run-time library functions
Embedded MATLAB run-time library functions are a subset of the functions that you can call in the MATLAB workspace. They generate C code for building targets that conform to the memory and data type requirements of embedded environments. In the preceding example, length, sqrt, and sum are examples of Embedded MATLAB run-time library functions. See Calling Embedded MATLAB Library Functions in the Embedded MATLAB User's Guide.
Stateflow functions
Graphical, truth table, and other Embedded MATLAB functions can be called from an Embedded MATLAB function in a Stateflow chart.
Some MATLAB functions
Functions that cannot be resolved as subfunctions, Embedded MATLAB run-time library functions, or Stateflow functions are resolved in the MATLAB workspace. These functions do not generate code; they execute only in the MATLAB workspace during simulation of the model. See Calling MATLAB Functions in the Embedded MATLAB User's Guide.
Fixed-Point Toolbox™ run-time library functions
For more information on fixed-point support in Embedded MATLAB functions, refer to Working with the Fixed-Point Embedded MATLAB Subset in the Fixed-Point Toolbox User's Guide.
Simulink® Design Verifier™ functions
Simulink Design Verifier software provides MATLAB functions for property proving and test generation:
![]() | Using Embedded MATLAB Functions in Stateflow Charts | Building a Simulink Model with an Embedded MATLAB Function | ![]() |

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