Main Content

Call Extrinsic MATLAB Functions in Stateflow Charts

Stateflow® charts in Simulink® models have an action language property that defines the syntax for state and transition actions. An icon in the lower-left corner of the chart canvas indicates the action language for the chart.

  • MATLAB® as the action language.

  • C as the action language.

In charts that use C as the action language, you can call built-in MATLAB functions and access MATLAB workspace variables by using the ml namespace operator or the ml function. For more information, see Access MATLAB Functions and Workspace Data in C Charts.

In charts that use MATLAB as the action language, you can call MATLAB functions supported for code generation directly. To call extrinsic functions that are not supported for code generation, you must use the coder.extrinsic (MATLAB Coder) function. When you declare a function with coder.extrinisic(function_name), Stateflow creates a call to the function during simulation. In a Stateflow chart, you only declare coder.extrinsic once. You cannot declare reserved keywords with coder.extrinsic. For more information, see Guidelines for Naming Stateflow Objects.

For charts that include atomic subcharts, you must declare functions that are not supported for code generation with coder.extrinsic separately within the atomic subchart.

Use the coder.extrinsic Function

To enable calls to the extrinsic function heaviside (Symbolic Math Toolbox), this model uses coder.extrinsic.

The chart contains two parallel states, A and B, and one graphical function block, foo. State A declares the function heaviside, which is not supported for code generation, by using coder.extrinsic. State B and the graphical function block also use heaviside without coder.extrinsic.

The input for state A is u1, a sine wave, and the input for state B is u2, a cosine wave. The graphical function out outputs the value of the heaviside function for the input in.

You only need to declare heaviside once in your chart using coder.extrinsic. After this you can use the heaviside function anywhere within your chart without coder.extrinsic. When generating code the functions that you declare using coder.extrinsic will have a call to the extrinsic function, and that function will not appear in the generated code.

To visualize the result of this chart, open the scope.

See Also

(Symbolic Math Toolbox) | (MATLAB Coder)

Related Topics