| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Real-Time Workshop Embedded Coder |
| Contents | Index |
| Learn more about Real-Time Workshop Embedded Coder |
| On this page… |
|---|
if (u1 > u2)
{
y1 = u1;
}
else
{
y1 = u2;
}One method to create an if-else statement is to use a Switch block from the Simulink > Signal Routing library.
Model If_Else_SL

Drag the Switch block from the Simulink>Signal Routing library into your model.
Connect the data inputs and outputs to the block.
Drag a Relational Operator block from the Logic & Bit Operations library into your model.
Connect the signals that are used in the if-expression to the Relational Operator block. The order of connection determines the placement of each signal in the if-expression.
Configure the Relational Operator block to be a greater than operator.
Connect the controlling input to the middle input port of the Switch block.
Double-click the Switch block and set Criteria for passing first input to u2~=0. This condition ensures that Simulink selects u1 if u2 is TRUE; otherwise u2 passes.
Results. Real-Time Workshop software generates the following If_Else_SL_step function in the file If_Else_SL.c:
/* External inputs (root inport signals with auto storage) */
ExternalInputs U;
/* External outputs (root outports fed by signals with auto storage) */
ExternalOutputs Y;
/* Model step function */
void If_Else_SL_step(void)
{
/* Switch: '<Root>/Switch' incorporates:
* Inport: '<Root>/u1'
* Inport: '<Root>/u2'
* Outport: '<Root>/y1'
* RelationalOperator: '<Root>/Relational Operator'
*/
if (U.u1 > U.u2) {
Y.y1 = U.u1;
} else {
Y.y1 = U.u2;
}
}
Model If_Else_SF

If-Else Stateflow Chart

Drag a Stateflow chart from the Stateflow library into your model. Follow the steps in Configuring Stateflow Charts.
Double-click the chart and select Tools > Explore or enter Ctrl+R to open the Model Explorer.
Add the inputs and outputs to the chart and specify their data type.
Connect the data inputs and outputs to the block.
Select Patterns > Add Decision > If-Else. The Stateflow Pattern dialog opens. Fill in the fields as follows:
| Description | If-Else (optional) |
| If condition | u1 > u2 |
| If action | y1 = u1 |
| Else action | y1 = u2 |
Results. Real-Time Workshop software generates the following If_Else_SF_step function in the file If_Else_SF.c:
/* External inputs (root inport signals with auto storage) */
ExternalInputs U;
/* External outputs (root outports fed by signals with auto storage) */
ExternalOutputs Y;
/* Model step function */
void If_Else_SF_step(void)
{
/* Stateflow: '<Root>/Chart' incorporates:
* Inport: '<Root>/u1'
* Inport: '<Root>/u2'
* Outport: '<Root>/y1'
*/
/* Gateway: Chart */
/* During: Chart */
/* Transition: '<S1>:14' */
/* If-Else */
if (U.u1 > U.u2) {
/* Transition: '<S1>:13' */
/* Transition: '<S1>:12' */
Y.y1 = U.u1;
/* Transition: '<S1>:11' */
} else {
/* Transition: '<S1>:10' */
Y.y1 = U.u2;
}
/* Transition: '<S1>:9' */
}
Model If_Else_EML

Add two Inport blocks and an Outport block to your model and configure them.
Drag anEmbedded MATLAB Function block from the Simulink User-defined Functions library into the model.
Double-click the block and the Embedded MATLAB editor opens. Edit the function to include the statement:
function y1 = fcn(u1, u2) if u1 > u2; y1 = u1; else y1 = u2; end
Connect the data inputs and output to the Embedded MATLAB Function block.
Click File > Save and close the Embedded MATLAB editor.
Save your model.
Results. Real-Time Workshop software generates the following If_Else_EML_step function in the file If_Else_EML.c:
/* External inputs (root inport signals with auto storage) */
ExternalInputs U;
/* External outputs (root outports fed by signals with auto storage) */
ExternalOutputs Y;
/* Model step function */
void If_Else_EML_step(void)
{
/* Embedded MATLAB: '<Root>/Embedded MATLAB Function' incorporates:
* Inport: '<Root>/u1'
* Inport: '<Root>/u2'
* Outport: '<Root>/y1'
*/
/* Embedded MATLAB Function 'Embedded MATLAB Function': '<S1>:1' */
if (U.u1 > U.u2) {
/* '<S1>:1:4' */
/* '<S1>:1:5' */
Y.y1 = U.u1;
} else {
/* '<S1>:1:6' */
Y.y1 = U.u2;
}
}
switch (u1)
{
case 2:
y1 = u2;
break;
case 3:
y1 = u3;
break;
default:
y1 = u4;
break;
}One method for creating a switch statement is to use a Switch Case block from the Simulink > Ports and Subsystems library.
Model Switch_SL

Drag a Switch Case block from the Simulink > Ports and Subsystems library into your model.
Double-click the block. In the Block parameters dialog box, fill in the Case Conditions parameter. In this example, the two cases are: {2,3}.
Select the Show default case parameter. The default case is optional in a switch statement.
Connect the condition input u1 to the input port of the Switch block.
Drag Switch Case Action Subsystem blocks from the Simulink>Ports and Subsystems library to correspond with the number of cases.
Configure the Switch Case Action Subsystem subsystems.
Drag a Merge block from the Simulink > Signal Routing library to merge the outputs.
The Switch Case block takes an integer input, therefore, the input signal u1 is type cast to an int32.
Results. Real-Time Workshop software generates the following Switch_SL_step function in the file Switch_SL.c:
/* Exported block signals */
int32_T u1; /* '<Root>/u1' */
/* External inputs (root inport signals with auto storage) */
ExternalInputs U;
/* External outputs (root outports fed by signals with auto storage) */
ExternalOutputs Y;
/* Model step function */
void Switch_SL_step(void)
{
/* SwitchCase: '<Root>/Switch Case' incorporates:
* ActionPort: '<S1>/Action Port'
* ActionPort: '<S2>/Action Port'
* ActionPort: '<S3>/Action Port'
* Inport: '<Root>/u1'
* SubSystem: '<Root>/Switch Case Action Subsystem'
* SubSystem: '<Root>/Switch Case Action Subsystem1'
* SubSystem: '<Root>/Switch Case Action Subsystem2'
*/
switch (u1) {
case 2:
/* Inport: '<S1>/u2' incorporates:
* Inport: '<Root>/u2'
* Outport: '<Root>/y1'
*/
Y.y1 = U.u2;
break;
case 3:
/* Inport: '<S2>/u3' incorporates:
* Inport: '<Root>/u3'
* Outport: '<Root>/y1'
*/
Y.y1 = U.u3;
break;
default:
/* Inport: '<S3>/u4' incorporates:
* Inport: '<Root>/u4'
* Outport: '<Root>/y1'
*/
Y.y1 = U.u4;
break;
}
}
Model Switch_EML

Add four Inport blocks and one Outport block to your model.
Drag an Embedded MATLAB Function block from the Simulink > User Defined Functions library into your model.
Double-click the Embedded MATLAB Function block. The Embedded MATLAB editor opens.
Edit the function to include the statement:
function y1 = fcn(u1, u2, u3, u4)
switch u1
case 2
y1 = u2;
case 3
y1 = u3;
otherwise
y1 = u4;
endConnect the data inputs and outputs to the Embedded MATLAB Function block.
Click File > Save and close the Embedded MATLAB editor.
Save your model.
Results. Real-Time Workshop software generates the following Switch_EML_step function in the file Switch_EML.c:
/* External inputs (root inport signals with auto storage) */
ExternalInputs U;
/* External outputs (root outports fed by signals with auto storage) */
ExternalOutputs Y;
/* Model step function */
void Switch_EML_step(void)
{
/* Embedded MATLAB: '<Root>/Embedded MATLAB Function' incorporates:
* Inport: '<Root>/u1'
* Inport: '<Root>/u2'
* Inport: '<Root>/u3'
* Inport: '<Root>/u4'
* Outport: '<Root>/y1'
*/
/* Embedded MATLAB Function 'Embedded MATLAB Function': '<S1>:1' */
/* '<S1>:1:4' */
switch (U.u1) {
case 2:
/* '<S1>:1:6' */
Y.y1 = U.u2;
break;
case 3:
/* '<S1>:1:8' */
Y.y1 = U.u3;
break;
default:
/* '<S1>:1:10' */
Y.y1 = U.u4;
break;
}
}
For an Embedded MATLAB Function block containing an if-elseif-else construct, you can select a configuration parameter for your model to convert the if-elseif-else to a switch statement. In the Configuration Parameters dialog box, on the Real-Time Workshop > Code Style pane, select the Convert if-elseif-else patterns to switch-case statements parameter. For more information, see Converting If-Elseif-Else Code to Switch-Case Statements in the Simulink documentation.
y1 = 0;
for(inx = 0; inx <10; inx++)
{
y1 = u1[inx] + y1;
}One method for creating a for loop is to use a For Iterator Subsystem block from the Simulink > Ports and Subsystems library.
Model For_Loop_SL

For Iterator Subsystem

Drag a For Iterator Subsystem block from the Simulink > Ports and Subsystems library into your model.
Connect the data inputs and outputs to the For Iterator Subsystem block.
Open the Inport block.
In the Block parameter dialog box, select the Signal Attributes pane and set the Port dimensions parameter to 10.
Double-click the For Iterator Subsystem block to open the subsystem.
Drag an Index Vector block from the Signal-Routing library into the subsystem.
Open the For Iterator block. In the Block parameter dialog box set the Index-mode parameter to Zero-based and the Iteration limit parameter to 10.
Connect the controlling input to the topmost input port of the Index Vector block, and the other input to the second port.
Drag an Add block from the Math Operations library into the subsystem.
Drag a Unit Delay block from Commonly Used Blocks library into the subsystem.
Double-click the Unit Delay block and set the Initial Conditions parameter to 0. This parameter initializes the state to zero.
Connect the blocks as shown in the model diagram.
Save the subsystem and the model.
Results. Real-Time Workshop software generates the following For_Loop_SL_step function in the file For_Loop_SL.c:
/* External inputs (root inport signals with auto storage) */
ExternalInputs U;
/* External outputs (root outports fed by signals with auto storage) */
ExternalOutputs Y;
/* Model step function */
void For_Loop_SL_step(void)
{
int32_T s1_iter;
int32_T rtb_y1;
/* Outputs for iterator SubSystem: '<Root>/For Iterator Subsystem' incorporates:
* ForIterator: '<S1>/For Iterator'
*/
for (s1_iter = 0; s1_iter < 10; s1_iter++) {
/* Sum: '<S1>/Add' incorporates:
* Inport: '<Root>/u1'
* MultiPortSwitch: '<S1>/Index Vector'
* UnitDelay: '<S1>/Unit Delay'
*/
rtb_y1 = U.u1[s1_iter] + DWork.UnitDelay_DSTATE;
/* Update for UnitDelay: '<S1>/Unit Delay' */
DWork.UnitDelay_DSTATE = rtb_y1;
}
/* end of Outputs for SubSystem: '<Root>/For Iterator Subsystem' */
/* Outport: '<Root>/y1' */
Y.y1 = rtb_y1;
}
Model For_Loop_SF


Drag a Stateflow chart from the Stateflow library into your model. Follow the steps in Configuring Stateflow Charts.
Double-click the chart and select Tools > Explore or enter Ctrl+R to open the Model Explorer.
Add the inputs and outputs to the chart and specify their data type.
Connect the data input and output to the Stateflow chart.
In the Model Explorer, select the output variable, then, in the right pane, select the Value Attributes tab and set the Initial Value to 0.
Select Patterns > Add Loop > For. The Stateflow Pattern dialog opens.
Fill in the fields in the Stateflow Pattern dialog box as follows:
| Description | For Loop (optional) |
| Initializer expression | inx = 0 |
| Loop test expression | inx < 10 |
| Counting expression | inx++ |
| For loop body | y1 = u1[inx] + y1 |
Results. Real-Time Workshop software generates the following For_Loop_SF_step function in the file For_Loop_SF.c:
/* Block signals (auto storage) */
BlockIO B;
/* External inputs (root inport signals with auto storage) */
ExternalInputs U;
/* External outputs (root outports fed by signals with auto storage) */
ExternalOutputs Y;
/* Model step function */
void For_Loop_SF_step(void)
{
int32_T sf_inx;
/* Stateflow: '<Root>/Chart' incorporates:
* Inport: '<Root>/u1'
*/
/* Gateway: Chart */
/* During: Chart */
/* Transition: '<S1>:24' */
/* For Loop */
/* Transition: '<S1>:25' */
for (sf_inx = 0; sf_inx < 10; sf_inx++) {
/* Transition: '<S1>:22' */
/* Transition: '<S1>:23' */
B.y1 = U.u1[sf_inx] + B.y1;
/* Transition: '<S1>:21' */
}
/* Transition: '<S1>:20' */
/* Outport: '<Root>/y1' */
Y.y1 = B.y1;
}
Model For_Loop_EML

Add an Inport and Outport block to your model.
Drag an Embedded MATLAB Function block from the Simulink > User Defined Functions library into your model.
Double-click the Embedded MATLAB Function block. The Embedded MATLAB editor opens.
Edit the function to include the statement:
function y1 = fcn(u1)
y1 = 0;
for inx=1:10
y1 = u1(inx) + y1 ;
endConnect the data inputs and outputs to the Embedded MATLAB Function block.
Click File > Save and close the Embedded MATLAB editor.
Save your model.
Results. Real-Time Workshop software generates the following For_Loop_EML_step function in the file For_Loop_EML.c:
/* Exported block signals */
real_T u1[10]; /* '<Root>/u1' */
real_T y1; /* '<Root>/Embedded MATLAB Function' */
/* Model step function */
void For_Loop_EML_step(void)
{
int32_T eml_inx;
/* Embedded MATLAB: '<Root>/Embedded MATLAB Function' incorporates:
* Inport: '<Root>/u1'
*/
/* Embedded MATLAB Function 'Embedded MATLAB Function': '<S1>:1' */
/* '<S1>:1:3' */
y1 = 0.0;
for (eml_inx = 0; eml_inx < 10; eml_inx++) {
/* '<S1>:1:5' */
/* '<S1>:1:6' */
y1 = u1[eml_inx] + y1;
}
}
while(flag && (num_iter <= 100)
{
flag = func ();
num_iter ++;
}One method for creating a while loop is to use a While Iterator Subsystem block from the Simulink > Ports and Subsystems library.
Model While_Loop_SL

While_Loop_SL/While Iterator Subsystem

Drag a While Iterator Subsystem block from the Simulink > Ports and Subsystems library into the model.
Drag a Constant block from the Simulink > Commonly Used Blocks library into the model. In this case, set the Initial Condition to 1 and the Data Type to Boolean. You do not always have to set the initial condition to FALSE. The initial condition can be dependent on the input to the block.
Connect the Constant block to the While Iterator Subystem block.
Double-click the While Iterator Subsystem block to open the subsystem.
Place a Subsystem block next to the While Iterator block.
Right-click the subsystem and select Subsystem Parameters. The Block parameters dialog box opens.
Select the Treat as atomic unit parameter to configure the subsystem to generate a function.
From the Real-Time Workshop system code list, select the option, Function.
From the Real-Time Workshop function name options list, select the option, User specified. The Real-Time Workshop function name parameter is displayed.
Specify the name as func.
Click Apply.
Double-click the func() subsystem block. In this example, function func() has an output flag set to 0 or 1 depending on the result of the algorithm in func( ). Create the func() algorithm as shown in the following diagram:
func()

Double-click the While Iterator block to set the Maximum number of iterations to 100.
Connect blocks as shown in the model and subsystem diagrams.
Results. Real-Time Workshop software generates the following While_Loop_SL_step function in the file While_Loop_SL.c:
/* Exported block signals */
boolean_T IC; /* '<Root>/Initial Condition SET to TRUE' */
boolean_T flag; /* '<S2>/Relational Operator' */
/* Block states (auto storage) */
D_Work DWork;
/* Start for atomic system: '<S1>/func( ) Is a function that updates the flag' */
void func_Start(void)
{
/* Start for RandomNumber: '<S2>/Random Number' */
DWork.RandSeed = 1144108930U;
DWork.NextOutput = rt_NormalRand(&DWork.RandSeed) * 1.7320508075688772E+000;
}
/* Output and update for atomic system:
* '<S1>/func( ) Is a function that updates the flag' */
void func(void)
{
/* RelationalOperator: '<S2>/Relational Operator' incorporates:
* Constant: '<S2>/Constant1'
* RandomNumber: '<S2>/Random Number'
*/
flag = (DWork.NextOutput > 1.0);
/* Update for RandomNumber: '<S2>/Random Number' */
DWork.NextOutput = rt_NormalRand(&DWork.RandSeed) * 1.7320508075688772E+000;
}
/* Model step function */
void While_Loop_SL_step(void)
{
int32_T s1_iter;
boolean_T loopCond;
/* Outputs for iterator SubSystem:
* '<Root>/While Iterator Subsystem' incorporates:
* WhileIterator: '<S1>/While Iterator'
*/
s1_iter = 1;
loopCond = IC;
while (loopCond && (s1_iter <= 100)) {
/* Outputs for atomic SubSystem:
* '<S1>/func( ) Is a function that updates the flag' */
func();
/* end of Outputs for SubSystem:
* '<S1>/func( ) Is a function that updates the flag' */
loopCond = flag;
s1_iter++;
}
/* end of Outputs for SubSystem: '<Root>/While Iterator Subsystem' */
}
Model While_Loop_SF

While_Loop_SF/Chart Executes the desired while-loop

Drag a Stateflow chart from the Stateflow library into your model. Follow the steps in Configuring Stateflow Charts.
Double-click the chart and select Tools > Explore or enter Ctrl+R to open the Model Explorer.
Add the inputs and outputs to the chart and specify their data type.
Connect the data input and output to the Stateflow chart.
In the Model Explorer, select the output variable, then, in the right pane, select the Value Attributes tab and set the Initial Value to 0.
Select Patterns > Add Loop > While. The Stateflow Pattern dialog opens.
Fill in the fields for the Stateflow Pattern dialog box as follows:
| Description | While Loop (optional) |
| While condition | (flag) && (num_iter<=100) |
| Do action | func; num_iter++; |
Place a Subsystem block in your model.
Right-click the subsystem and select Subsystem Parameters. The Block parameters dialog box opens.
Select the Treat as atomic unit parameter to configure the subsystem to generate a function.
From the Real-Time Workshop system code list, select the option, Function.
From the Real-Time Workshop function name options list, select the option, User specified. The Real-Time Workshop function name parameter is displayed.
Specify the name as func.
Click Apply to apply all changes.
Double-click the func() subsystem block. In this example, function func() has an output flag set to 0 or 1 depending on the result of the algorithm in func( ). The Trigger block parameter Trigger type is function-call. Create the func() algorithm, as shown in the following diagram:
While_Loop_SF/func() A function that updates the flag

Save and close the subsystem.
Connect blocks to the Stateflow chart as shown in the model diagram for While_Loop_SF.
Save your model.
Results. Real-Time Workshop software generates the following While_Loop_SF_step function in the file While_Loop_SF.c:
/* Exported block signals */
int32_T num_iter; /* '<Root>/Chart Executes the desired while-loop' */
boolean_T flag; /* '<S2>/Relational Operator' */
/* Block states (auto storage) */
D_Work DWork;
/* Model step function */
void While_Loop_SF_step(void)
{
/* Stateflow: '<Root>/Chart Executes the desired
* while-loop' incorporates:
* SubSystem: '<Root>/func() A function that
* updates the flag'
*/
/* Gateway: Chart
Executes the desired while-loop */
/* During: Chart
Executes the desired while-loop */
/* Transition: '<S1>:2' */
num_iter = 1;
while (flag && (num_iter <= 100)) {
/* Transition: '<S1>:3' */
/* Transition: '<S1>:4' */
/* Event: '<S1>:12' */
func();
num_iter = num_iter + 1;
/* Transition: '<S1>:5' */
}
/* Transition: '<S1>:1' */
}
Model While_Loop_EML

Add an Inport and Outport block to your model.
In the Simulink Library Browser, click Simulink > User Defined Functions, and drag an Embedded MATLAB Function block into your model.
Double-click the Embedded MATLAB Function block. The Embedded MATLAB editor opens.
Edit the function to include the statement:
function fcn(func_flag)
flag = true;
num_iter = 1;
while(flag && (num_iter<=100))
func;
flag = func_flag;
num_iter = num_iter + 1;
endClick File > Save and close the Embedded MATLAB editor.
Place a Subsystem block in your model, right-click the subsystem and select Subsystem Parameters. The Block parameters dialog box opens.
Select the Treat as atomic unit parameter to configure the subsystem to generate a function.
From the Real-Time Workshop system code list, select the option, Function.
From the Real-Time Workshop function name options list, select the option, User specified. The Real-Time Workshop function name parameter is displayed.
Specify the name as func.
Click Apply.
Double-click the func() subsystem block. In this example, function func() has an output flag set to 0 or 1 depending on the result of the algorithm in func( ). The Trigger block parameter Trigger type is function-call. Create the func() algorithm, as shown in the following diagram:

Save and close the subsystem.
Connect the Embedded MATLAB Function block to the func() subsystem.
Save your model.
Results. Real-Time Workshop software generates the following While_Loop_EML_step function in the file While_Loop_EML.c. In some cases an equivalent for loop might be generated instead of a while loop.
/* Exported block signals */
boolean_T func_flag; /* '<S2>/Relational Operator' */
/* Block states (auto storage) */
D_Work DWork;
/* Model step function */
void While_Loop_EML_step(void)
{
boolean_T eml_func_flag;
boolean_T eml_flag;
int32_T eml_num_iter;
/* Embedded MATLAB: '<Root>/Embedded MATLAB Function Executes
* the desired While-Loop' incorporates:
* SubSystem: '<Root>/func() updates the "flag"'
*/
eml_func_flag = func_flag;
/* Embedded MATLAB Function 'Embedded MATLAB Function
* Executes the desired While-Loop': '<S1>:1' */
/* '<S1>:1:3' */
eml_flag = true;
/* '<S1>:1:4' */
for (eml_num_iter = 1; eml_flag && (eml_num_iter <= 100);
eml_num_iter++) {
/* '<S1>:1:6' */
/* '<S1>:1:7' */
func();
/* '<S1>:1:8' */
eml_flag = eml_func_flag;
/* '<S1>:1:9' */
}
}
![]() | Standard Methods to Prepare a Model for Code Generation | Defining Data Representation and Storage for Code Generation | ![]() |

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 |