| Contents | Index |
| On this page… |
|---|
If you want to generate standalone code for applications other than production or rapid prototyping, you can use Simulink Coder code generation software to build a custom target.
For information on setting custom target options programmatically, see Target Properties and Target Methods in the Stateflow API.
To add a custom target to your model:
In the Contents pane of the Model Explorer, right-click the row of the custom target and select Properties.
The Stateflow Custom Target dialog box appears.

In the Name field, enter any name except the reserved names sfun and rtw. Then click OK.
To configure a custom target:
The chart appears highlighted in the Model Hierarchy pane.

In the Model Hierarchy pane, select the main model with the custom target.

The custom target (in this example, ctarg) appears as an object of the main model.
In the Contents pane, click the row for the custom target.
The Stateflow Custom Target dialog box appears in the pane on the right.

In the General pane of the Stateflow Custom Target dialog box, specify options for your custom target:
User Comments in generated code — Includes user-defined comments in the generated code.
Auto-generated Comments in generated code — Includes auto-generated comments in the generated code.
State/Transition Descriptions in generated code — Includes descriptions of states and transitions in the generated code.
Use bitsets for storing state configuration — Reduces the amount of memory that stores the variables. However, it can increase the amount of memory that stores target code if the target processor does not include instructions for manipulating bitsets.
Use bitsets for storing boolean data — Reduces the amount of memory that stores Boolean variables. However, it can increase the amount of memory that stores target code if the target processor does not include instructions for manipulating bitsets.
Compact nested if-else using logical AND/OR operators — Improves readability of generated code by compacting nested if-else statements using logical AND (&&) and OR (||) operators.
For example, the generated code
if(c1) {
if(c1) {
a1();
}
}
becomes
if(c1 && c2) {
a1();
}
and the generated code
if(c1) {
/* fall through to do a1() */
}else if(c2) {
/* fall through to do a1() */
}else{
/* skip doing a1() */
goto label1;
}
a1();
label1:
a2();
becomes
if(c1 || c2) {
a1();
}
a2();
Recognize if-elseif-else in nested if-else statements — Improves readability of generated code by creating an if-elseif-else construct in place of deeply nested if-else statements.
For example, the generated code
if(c1) {
a1();
}else{
if(c2) {
a2();
}else{
if(c3) {
a3();
}
}
}
becomes
if(c1) {
a1();
}else if(c2) {
a2();
}else if(c3) {
a3();
}
Replace constant expressions by a single constant — Improves readability by preevaluating constant expressions and replacing them with a single constant. This optimization also eliminates dead code.
For example, the generated code
if(2+3<2) {
a1();
}else {
a2(4+5);
}
becomes
if(0) {
a1();
}else {
a2(9);
}
in the first phase of this optimization. The second phase eliminates the if statement, resulting in simply
a2(9);
Minimize array reads using temporary variables — Minimizes expensive array read operations by using temporary variables when possible.
For example, the generated code
a[i] = foo();
if(a[i]<10 && a[i]>1) {
y = a[i]+5;
}else{
z = a[i];
}
becomes
a[i] = foo();
temp = a[i];
if(temp<10 && temp>1) {
y = temp+5;
}else{
z = temp;
}
Use chart names with no mangling — (See the note below before using.) Preserves the names of chart entry functions so that you can invoke them using handwritten C code.
Note When you select this check box, the generated code does not mangle the chart names to make them unique. Because this option does not check for name conflicts in generated code, use this option only when you have unique chart names in your model. Conflicts in generated names can cause variable aliasing and compilation errors. |
I/O data format — Choose one of these options:
Select Use global input/output data to generate chart input and output data as global variables.
Select Pack input/output data into structures to generate structures for chart input data and chart output data.
Generate chart initializer function — Generates a function initializer of data.
Multi-instance capable code — Generates multiple instantiable chart objects instead of a static definition.
Select one of the following build options:
Generate Code Only (non-incremental) to regenerate code for all charts in the model.
Rebuild All (including libraries) to rebuild the target, including chart libraries, from scratch. Use this option if you have changed your compiler or updated your object files since the last build.
Make without generating code to invoke the make process without generating code. Use this option when you have custom source files that you must recompile in an incremental build mechanism that does not detect changes in custom code files.
Specify any custom code options in the Custom Code pane.
To build a custom target, click Execute in the General pane of the Stateflow Custom Target dialog box. See Generated Code Files for Targets You Build for details about the code you generate for this target and the folder structure.
You cannot build a custom target if your chart contains any of the following items:
Enumerated data (see Using Enumerated Data in Stateflow Charts)
Atomic subcharts (see Making States Reusable with Atomic Subcharts)
![]() | Examples of Integrating Custom C Code in Nonlibrary Models | What Happens During the Target Building Process? | ![]() |

Learn how engineers use Stateflow to model state machines in their Simulink models.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |