Main Content

Access Custom C++ Code in Stateflow Charts

This example shows how to integrate custom C++ code with Stateflow® charts in Simulink® models. By sharing data and functions between your custom code and your Stateflow charts, you can augment the capabilities of Stateflow and take advantage of your preexisting code. For more information, see Reuse Custom Code in Stateflow Charts.

In this example, a chart that uses C as the action language calls a custom code function named adderOutput. This function increments the value of the global custom variable adderVar by a specified amount. The chart stores the output of this function as the chart output counter. When counter is less than or equal to 100, the chart calls the custom function to increase the global variable by the chart input increment. When counter is greater than 100, the chart calls the custom function to decrease the global variable by the chart input increment.

Prepare Custom Code Files

Add a C function wrapper to execute each method in your C++ code. For instance, in this example, the C++ source file adder_cpp.cpp defines a class called adder that has two methods, add_one and get_val.

adder::adder()
{
	int_state = 0;
}
int adder::add_one(int increment)
{
	   int_state += increment;
    return int_state;
}
int adder::get_val()
{
    return int_state;
}

To call these methods, the Stateflow chart uses the C function wrapper adderOutput.

double adderOutput(adder *obj, int increment)
{
	   obj->add_one(increment);
	   return obj->get_val();
}

The header file adder_cpp.h contains a prototype of this C function wrapper:

extern double adderOutput(adder *obj, int increment);

Choose a C++ Compiler

To view or change the default compiler, in the MATLAB® Command Window, enter:

mex -setup c++

For more information, see Choose a C++ Compiler. For a list of supported compilers, see Supported and Compatible Compilers.

Include Custom Code Files for Simulation

Configure your simulation target and select C++ as the custom code language:

  1. Open the Configuration Parameters dialog box.

  2. In the Simulation Target pane, set the Language parameter to C++.

  3. In the Code Information tab, specify your header and source files, as described in Configure Custom Code.

When you simulate the model, the Scope block shows the value of the chart output counter increase or decrease by the value of the chart input increment.

C++ Code Generation

Select C++ as the code generation language and configure your model to use the same custom code settings specified for the simulation target:

  1. Open the Configuration Parameters dialog box.

  2. In the Code Generation pane, set the Language parameter to C++.

  3. In the Code Generation > Custom Code pane, select Use the same custom code settings as Simulation Target.

  4. Generate C++ code as described in Generate Code Using Simulink Coder (Simulink Coder) or Generate Code Using Embedded Coder (Embedded Coder).

Related Topics

External Websites