| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Simulink |
| Contents | Index |
| Learn more about Simulink |
To use readability optimizations in your code, you must have a Real-Time Workshop Embedded Coder license. These optimizations appear only in code that you generate for an embedded real-time (ert) target.
Note These optimizations do not apply to M-files that you call from an Embedded MATLAB Function block. |
For more information, see Choosing and Configuring an Embedded Real-Time Target and Controlling Code Style in the Real-Time Workshop Embedded Coder documentation.
When you generate code for embedded real-time targets, you can choose to convert if-elseif-else decision logic to switch-case statements. This conversion can enhance readability of the code.
For example, when an Embedded MATLAB Function block contains a long list of conditions, the switch-case structure:
Reduces the use of parentheses and braces
Minimizes repetition in the generated code
The following procedure describes how to convert generated code for the Embedded MATLAB Function block from if-elseif-else to switch-case statements.
| Step | Task | Reference |
|---|---|---|
| 1 | Verify that your block follows the rules for conversion. | Verifying the Contents of the Block |
| 2 | Enable the conversion. | Enabling the Conversion |
| 3 | Generate code for your model. | Generating Code for Your Model |
For the conversion to occur, the following rules must hold. LHS and RHS refer to the left-hand side and right-hand side of a condition, respectively.
| Construct | Rules to Follow |
|---|---|
| Embedded MATLAB Function block | Must have two or more unique conditions, in addition to a default. For more information, see How the Conversion Handles Duplicate Conditions. |
| Each condition | Must test equality only. |
Must use the same variable or expression for the LHS. | |
| Each LHS | Must be a single variable or expression, not a compound statement. |
Cannot be a constant. | |
Must have an integer or enumerated data type. | |
Cannot have any side effects on simulation. For example, the LHS can read from but not write to global variables. | |
| Each RHS | Must be a constant. |
Must have an integer or enumerated data type. |
If an Embedded MATLAB Function block has duplicate conditions, the conversion preserves only the first condition. The generated code discards all other instances of duplicate conditions.
After removal of duplicates, two or more unique conditions must exist. Otherwise, no conversion occurs and the generated code contains all instances of duplicate conditions.
The following examples show how the conversion handles duplicate conditions.
| Example of Generated Code | Code After Conversion |
|---|---|
if (x == 1) {
block1
} else if (x == 2) {
block2
} else if (x == 1) { // duplicate
block3
} else if (x == 3) {
block4
} else if (x == 1) { // duplicate
block5
} else {
block6
} | switch (x) {
case 1:
block1; break;
case 2:
block2; break;
case 3:
block4; break;
default:
block6; break;
} |
if (x == 1) {
block1
} else if (x == 1) { // duplicate
block2
} else {
block3
} | No change, because only one unique condition exists |
Suppose that you have the following model with an Embedded MATLAB Function block. Assume that the output data type is double and the input data type is Controller, an enumerated type that you define. (See How to Define Enumerated Data Types for Embedded MATLAB Function Blocks for more information.)

The block contains the following code:
function system = fcn(type)
%#eml
if (type == Controller.P)
system = 0;
elseif (type == Controller.I)
system = 1;
elseif (type == Controller.PD)
system = 2;
elseif (type == Controller.PI)
system = 3;
elseif (type == Controller.PID)
system = 4;
else
system = 10;
endThe enumerated type definition in Controller.m is:
classdef(Enumeration) Controller < Simulink.IntEnumType
enumeration
P(0)
I(1)
PD(2)
PI(3)
PID(4)
UNKNOWN(10)
end
endIf you generate code for an embedded real-time target using default settings, you see something like this:
if (if_to_switch_eml_blocks_U.In1 == P) {
/* '<S1>:1:4' */
/* '<S1>:1:5' */
if_to_switch_eml_blocks_Y.Out1 = 0.0;
} else if (if_to_switch_eml_blocks_U.In1 == I) {
/* '<S1>:1:6' */
/* '<S1>:1:7' */
if_to_switch_eml_blocks_Y.Out1 = 1.0;
} else if (if_to_switch_eml_blocks_U.In1 == PD) {
/* '<S1>:1:8' */
/* '<S1>:1:9' */
if_to_switch_eml_blocks_Y.Out1 = 2.0;
} else if (if_to_switch_eml_blocks_U.In1 == PI) {
/* '<S1>:1:10' */
/* '<S1>:1:11' */
if_to_switch_eml_blocks_Y.Out1 = 3.0;
} else if (if_to_switch_eml_blocks_U.In1 == PID) {
/* '<S1>:1:12' */
/* '<S1>:1:13' */
if_to_switch_eml_blocks_Y.Out1 = 4.0;
} else {
/* '<S1>:1:15' */
if_to_switch_eml_blocks_Y.Out1 = 10.0;
}
The LHS variable if_to_switch_eml_blocks_U.In1 appears multiple times in the generated code.
Note By default, variables that appear in the block do not retain their names in the generated code. Modified identifiers guarantee that no naming conflicts occur. |
Traceability comments appear between each set of /* and */ markers. To learn more about traceability, see Using Traceability in Embedded MATLAB Function Blocks.
Check that the block follows all the rules in Rules for Conversion.
| Construct | How the Construct Follows the Rules |
|---|---|
| Embedded MATLAB Function block | Five unique conditions exist, in addition to the default:
|
| Each condition | Each condition:
|
| Each LHS | Each LHS:
|
| Each RHS | Each RHS:
|
Open the Configuration Parameters dialog box.
In the Real-Time Workshop pane, select ert.tlc for the System target file.
This step specifies an embedded real-time target for your model.
In the Real-Time Workshop > Code Style pane, select the Convert if-elseif-else patterns to switch-case statements check box.
Tip This conversion works on a per-model basis. If you select this check box, the conversion applies to:
For more information, see Enhancing Readability of Generated Code for Flow Graphs in the Stateflow documentation. |
In the Real-Time Workshop pane of the Configuration Parameters dialog box, click Build in the lower right corner.
The code for the Embedded MATLAB Function block uses switch-case statements instead of if-elseif-else code:
switch (if_to_switch_eml_blocks_U.In1) {
case P:
/* '<S1>:1:4' */
/* '<S1>:1:5' */
if_to_switch_eml_blocks_Y.Out1 = 0.0;
break;
case I:
/* '<S1>:1:6' */
/* '<S1>:1:7' */
if_to_switch_eml_blocks_Y.Out1 = 1.0;
break;
case PD:
/* '<S1>:1:8' */
/* '<S1>:1:9' */
if_to_switch_eml_blocks_Y.Out1 = 2.0;
break;
case PI:
/* '<S1>:1:10' */
/* '<S1>:1:11' */
if_to_switch_eml_blocks_Y.Out1 = 3.0;
break;
case PID:
/* '<S1>:1:12' */
/* '<S1>:1:13' */
if_to_switch_eml_blocks_Y.Out1 = 4.0;
break;
default:
/* '<S1>:1:15' */
if_to_switch_eml_blocks_Y.Out1 = 10.0;
break;
}
The switch-case statements provide the following benefits to enhance readability:
The code reduces the use of parentheses and braces.
The LHS variable if_to_switch_eml_blocks_U.In1 appears only once, minimizing repetition in the code.
![]() | Using Traceability in Embedded MATLAB Function Blocks | Speeding Up Simulation with the Basic Linear Algebra Subprograms (BLAS) Library | ![]() |

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 |