| 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.
Enter Ctrl+B to build the model and generate code.
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;
}
}
If-Else Stateflow Chart

Follow the steps forSetting Up an Example Model With a Stateflow Chart. This example model contains two Inport blocks and one Outport block.
Name your model If_Else_SF.
When configuring your Stateflow chart, 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 |
Press Ctrl+B to build the model and generate code.
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' */
}
Follow the steps for Setting Up an Example Model With an Embedded MATLAB Block. This example model contains two Inport blocks and one Outport block.
Name your model If_Else_EML.
In the Embedded MATLAB editor enter the function, as follows:
function y1 = fcn(u1, u2) if u1 > u2; y1 = u1; else y1 = u2; end
Press Ctrl+B to build the model and generate code.
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.
Enter Ctrl+B to build the model and generate code.
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;
}
}
Follow the steps for Setting Up an Example Model With an Embedded MATLAB Block. This example model contains four Inport blocks and one Outport block.
Name your model If_Else_EML.
In the Embedded MATLAB editor enter the function, as follows:
function y1 = fcn(u1, u2, u3, u4)
switch u1
case 2
y1 = u2;
case 3
y1 = u3;
otherwise
y1 = u4;
endPress Ctrl+B to build the model and generate code.
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;
}
}
If an Embedded MATLAB Function block or a Stateflow chart contains uses if-elseif-else decision logic, you can convert it to a switch statement by using a configuration parameter. 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. For more information on this conversion using a Stateflow chart, see Converting If-Elseif-Else Code to Switch-Case Statements and Example of Converting Code for If-Elseif-Else Decision Logic to Switch-Case Statements in the Stateflow 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 Parameters 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 Parameters 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.
Enter Ctrl+B to build the model and generate code.
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;
}

Follow the steps forSetting Up an Example Model With a Stateflow Chart. This example model contains one Inport block and one Outport block.
Name the model For_Loop_SF.
Enter Ctrl+R to open the Model Explorer.
In the Model Explorer, select the output variable, u1, and in the right pane, select the Value Attributes tab and set the Initial Value to 0.
In the Stateflow Editor, 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 |
The Stateflow diagram is shown.
Press Ctrl+B to build the model and generate code.
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;
}
Follow the directions for Setting Up an Example Model With an Embedded MATLAB Block. This example model contains one Inport block and one Outport block.
Name your model For_Loop_EML.
In the Embedded MATLAB editor enter the function, as follows:
function y1 = fcn(u1)
y1 = 0;
for inx=1:10
y1 = u1(inx) + y1 ;
endPress Ctrl+B to build the model and generate code.
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

Add a Stateflow Chart to your model from the Stateflow > Stateflow Chart library.
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 as shown in the model diagram.
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

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.
In the Embedded MATLAB editor enter the function, as follows:
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' */
}
}
num_iter = 1;
do {
flag = func();
num_iter++;
}
while (flag && num_iter <= 100)One method for creating a while loop is to use a While Iterator Subsystem block from the Simulink > Ports and Subsystems library.
Do_While_Loop_SL

Do_While_Loop_SL/While Iterator Subsystem

Drag a While Iterator Subsystem block from the Simulink > Ports and Subsystems library into the model.
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:
Do_While_Loop_SL/While Iterator Subsystem/func()

Double-click the While Iterator block. This opens the Block Parameters dialog.
Set the Maximum number of iterations to 100.
Specify the While loop type as do-while.
Connect blocks as shown in the model and subsystem diagrams.
Enter Ctrl+B to generate code.
void func(void)
{
flag = (DWork.NextOutput > (real_T)P.Constant1_Value);
DWork.NextOutput =
rt_NormalRand(&DWork.RandSeed) * P.RandomNumber_StdDev +
P.RandomNumber_Mean;
}
void Do_While_Loop_SL_step(void)
{
int32_T s1_iter;
s1_iter = 1;
do {
func();
s1_iter++;
} while (flag && (s1_iter <= 100));
}Do_While_Loop_SF

Do_While_Loop_SF/Chart Executes the desired do-while-loop

Add a Stateflow Chart to your model from the Stateflow > Stateflow Chart library.
Double-click the chart and select Tools > Explore or enter Ctrl+B 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:
Do_While_Loop_SF/func() Updates the flag

Save and close the subsystem.
Connect blocks to the Stateflow chart as shown in the model diagram for Do_While_Loop_SF.
Save your model.
void Do_While_Loop_SF_step(void)
{
int32_T sf_num_iter;
do {
sf_num_iter = 1;
func();
sf_num_iter++;
} while (flag && (sf_num_iter <= 100));
}
![]() | Types, Operators, and Expressions | Functions | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2010- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |