Skip to Main Content Skip to Search
Product Documentation

Types, Operators, and Expressions

Data Declaration

C Construct

int32 p1 = 3;

Declare a Variable for a Block Parameter Using a Data Object

You can specify certain block parameters as a variable. If you define the variable as a data object, the variable is global. Where the variable is declared in the generated code depends on the custom storage class that you choose (and whether you select Inline Parameters on the Optimization > Signals and Parameters pane ). If you choose Inline Parameters, then the data object name is used in the generated code. If you did not choose Inline Parameters, the generated code creates a global structure that stores all of the parameters. For more information on how to create a data object, see Data, Function, and File Definition.

BlockParameter
ConstantValue
GainValue
For IteratorIteration Limit

There are several methods for configuring data objects:

The following example demonstrates how to create a data object using the Model Explorer. The declaration statement in the generated code is as follows:

int Kp = 3;
  1. Create a model containing a Constant block and a Gain block.

  2. Press Ctrl+E to open the Configuration Parameters dialog box.

  3. On the Optimization > Signals and Parameters pane of the Configuration Parameter dialog box, select Inline parameters.

  4. Click Apply and OK. The Configuration Parameter dialog box closes.

  5. In your model, double-click the Constant block. The Block Parameters dialog box opens.

  6. In the Value field, enter a variable name. In this example, the variable name is p1.

  7. In your model, double-click the Gain block. The Block Parameters dialog box opens.

  8. In the Value field, enter a variable name. In this example, the variable name is p2.

  9. Press Ctrl+H to open the Model Explorer. On the Model Hierarchy pane, select the base workspace.

  10. To add two MPT parameter objects, in the menu bar, select Add > MPT Parameter in the menu bar twice. On the Contents of: Base Workspace pane, you see the parameters.

  11. Double-click each mpt.Parameter object and change their names to p1 and p2.

  12. Click the p1 parameter. The data object parameters are displayed in the right pane of the Model Explorer.

  13. In the Value field, enter 3 for p1. For the Data type, select int32. Because you chose an MPT parameter, the Storage Class is already set to Global(Custom).

  14. In the Value field, enter 5 for p2. For the Data type, select int32.

  15. Press Ctrl+B to generate code.

In the model.c file you see:

int32 p1 = 3;
int32 p2 = 5;

C Construct

int p1 = 3;

Declare a Variable for a Signal using a Data Object

  1. Create a model and label the signals.

  2. In the model tool bar, click Tools > Data Object Wizard to open the Data Object Wizard. If you are not familiar with creating Simulink Data Objects using the wizard, refer to Data Object Wizard .

  3. Click Find. The list of unresolved parameters and objects populates the Data Object Wizard. You can do mass edits for identical data objects.

  4. Select the signals individually or select all signals by clicking Check All.

  5. From the parameter Choose package for selected data objects drop-down list, select the mpt package. Click Apply Package. When you open the Model Explorer the data objects appear in the base workspace.

  6. In the base workspace, click the p1 data object . The data object parameters appear in the right pane of the Model Explorer.

  7. From the Data type drop-down list, select int16.

  8. You can also specify the storage class. The data object is an mpt.Parameter object, therefore the Storage Class is automatically set to Global (Custom).

      Note   The Storage class alters the data object implementation in the generated code. For more information, see Signal Objects.

Data Type Conversion

C Construct

 y1 = (double)u1;

Modeling Patterns

Modeling Pattern for Data Type Conversion — Simulink Block

One method to create a data type conversion is to use a Data Type Conversion block from the Simulink > Commonly Used Blocks library.

ex_data_type_SL

  1. From the Commonly Used Blocks library, drag a Data Type Conversion block into your model and connect to the Inport and Outport blocks.

  2. Double-click on the Data Type Conversion block to open the Block Parameters dialog box.

  3. Select the Output data type parameter as double.

  4. Press Ctrl+B to build the model and generate code.

The generated code appears in ex_data_type_SL.c, as follows:

int32_T u1;
real_T y1;

void ex_data_type_SL_step(void)
{
 y1 = (real_T)u1;
}

The Embedded Coder type definition for double is real_T.

Modeling Pattern for Data Type Conversion — Stateflow Chart

Stateflow Chart Type Conversion

Procedure.  

  1. Follow the steps for Set Up an Example Model With a Stateflow Chart . This example contains one Inport block and one Outport block.

  2. Name the example model ex_data_type_SF.

  3. Double-click the Inport block and select the Signal Attributes tab. Specify the Data Type as int32 from the drop down menu.

  4. Double-click the Outport block and select the Signal Attributes tab. Specify the Data Type as Inherit: auto from the drop down menu.

  5. In the Stateflow Editor, specify the Data Type for y1 as Boolean

  6. Press Ctrl+B to build the model and generate code.

Results.  The generated code appears in ex_data_type_SF.c, as follows:

int32_T u1;
real_T y1;
void ex_data_type_SF_step(void)
{
  y1 = (real_T)u1;
}

Modeling Pattern for Data Type Conversion — MATLAB Function Block

Procedure.  

  1. Follow the steps for Set Up an Example Model With a MATLAB Function Block . This example model contains one Inport block and one Outport block.

  2. Name the model ex_data_type_ML_Func.

  3. In the MATLAB Function Block Editor enter the function, as follows:

    function y1 = typeconv(u1)
    y1 = double(u1); 
    end
  4. Press Ctrl+B to build the model and generate code.

Results.  The generated code appears in ex_data_type_ML_func.c, where real32_T is a float and real_T is a double. Type conversion occurs across assignments.

real32_T u1;
real_T y1;

void ex_data_type_ML_func_step(void)
{
     y1 = u1;
}

Other Type Conversions in Modeling

Type conversions can also occur on the output of blocks where the output variable is specified as a different data type. For example, in the Gain block, you can select the Inherit via internal rule parameter to control the output signal data type. Another example of type conversion can occur at the boundary of a Stateflow chart. You can specify the output variable as a different data type.

Type Qualifiers

Modeling Patterns for Type Qualifiers

Using a Tunable Parameter in the Base Workspace

A tunable parameter is a block parameter whose value can be changed at runtime. The storage class property of a parameter specifies how the parameter is declared in the generated code.

ex_type_qual

Procedure.  

  1. Create a model containing a Constant block and an Outport block.

  2. Double-click the Constant block. In the Constant value field, enter the parameter name p1 .

  3. In the base workspace, create a MATLAB variable for p1 and specify its Value as 9.8 and its Data type as double.

  4. Press Ctrl+E to open the Configuration Parameters dialog box and select the Optimization > Signals and Parameters pane.

  5. Select the Inline parameters parameter, which activates the Configure button.

  6. Click the Configure button to open the Model Parameter Configuration dialog box.

  7. To declare a tunable parameter, from the Source list, select the variable p1.

  8. Click the Add to table button to add p1 to the Global (tunable) parameters section.

  9. Click the Storage Class and select Exported Global.

  10. Click the Storage Type Qualifier arrow and select const.

  11. Click Apply to save all of the changes.

  12. Press Ctrl+B to build the model and generate code.

Results.  The generated code appears in ex_type_qual.c as follows:

/* Exported block parameters */
   const real_T p1 = 9.8;         /* Variable: p1
                                   * Referenced by: '<Root>/Constant'
                                   */

Use a Data Object of the Const Custom Storage Class

One way to create a type qualifier in the generated code is to create a data object and specify a custom storage class. Use the previous model, ex_type_qual, for this example. Specify p1 differently as follows:

Procedure.  

  1. Press Ctrl+H to open the Model Explorer. On the Model Hierarchy pane, select the base workspace.

  2. In the menu bar, select Add > MPT Parameter to add an MPT parameter object. The parameter is displayed in the Contents of: Base Workspace pane.

  3. Double-click the mpt.Parameter object and change the Name to p1.

  4. Click the p1 parameter which displays the data object parameters on the right pane of the Model Explorer.

  5. In the Value field, enter 9.8 for p1. Specify the Data type as auto for 64–bit double.

  6. You can use the different type qualifiers by selecting a custom storage class from the Storage class list. For this example, select ConstVolatile (custom).

  7. In the Configuration Parameters dialog box, on the Optimization > Signals and Parameters pane, select the Inline parameters.

  8. Press Ctrl+B to build the model and generate code.

Results.  The generated code produces the type qualifier in ex_type_qual.c:

const volatile real_T p1 = 9.8;

Relational and Logical Operators

Modeling Patterns for Relational and Logical Operators

Modeling Pattern for Relational or Logical Operators — Simulink Blocks

ex_logical_SL

Procedure.  

  1. From the Logic and Bit Operations library, drag a Logical Operator block into your model.

  2. Double-click the block to configure the logical operation. Set the Operator field to OR.

  3. Name the blocks, as shown in the model ex_logical_SL.

  4. Connect the blocks and name the signals, as shown in the model ex_logical_SL.

  5. Press Ctrl+B to build the model and generate code.

Results.  Code implementing the logical operator OR is in the ex_logical_SL_step function in ex_logical_SL.c:

/* Exported block signals */
   boolean_T u1;                       /* '<Root>/u1' */
   boolean_T u2;                       /* '<Root>/u2' */
   boolean_T y1;                       /* '<Root>/Logical Operator'*/
      
   /* Logic: '<Root>/Logical Operator' incorporates:
    *  Inport: '<Root>/u1'
    *  Inport: '<Root>/u2'
    */
    y1 = (u1 || u2);

Modeling Pattern for Relational and Logical Operators —Stateflow Chart

ex_logical_SF/Logical Operator Stateflow Chart

Procedure.  

  1. Follow the steps for Set Up an Example Model With a Stateflow Chart. This example model contains two Inport blocks and one Outport block.

  2. Name the example model ex_logical_SF.

  3. In the Stateflow Editor, specify the Data Type for y1 as Boolean.

  4. In the Stateflow Editor, create the Stateflow diagram as shown. The relational or logical operation actions are on the transition from one junction to another. Relational statements specify conditions to conditionally allow a transition. In that case, the statement would be within square brackets.

  5. Press Ctrl+B to build the model and generate code.

Results.  Code implementing the logical operator OR is in the ex_logical_SF_step function in ex_logical_SF.c:

boolean_T u1;               /* '<Root>/u1' */
boolean_T u2;               /* '<Root>/u2' */
boolean_T y1;               /* '<Root>/Chart' */

void ex_logical_SF_step(void)
{
    y1 = (u1 || u2);
}

Modeling Pattern for Relational and Logical Operators — MATLAB Function Block

This example demonstrates the MATLAB Function block method for incorporating operators into the generated code using a relational operator.

Procedure.  

  1. Follow the steps for Set Up an Example Model With a MATLAB Function Block . This example model contains two Inport blocks and one Outport block.

  2. Name the example model ex_rel_operator_ML.

  3. In the MATLAB Function Block Editor enter the function, as follows:

    function y1 = fcn(u1, u2)
    y1 = u1 > u2; 
    end
  4. Press Ctrl+B to build the model and generate code.

Results.  Code implementing the relational operator '>' is in the ex_rel_operator_ML_step function in ex_rel_operator_ML.c:

real_T u1;                        /* '<Root>/u1' */
real_T u2;                        /* '<Root>/u2' */
boolean_T y;                      /* '<Root>/MATLAB Function' */

void ex_rel_operator_ML_step(void)
{
  y = (u1 > u2);
 }

Bitwise Operations

Simulink Bitwise-Operator Block

ex_bit_logic_SL

Procedure.  

  1. Drag a Bitwise Operator block from the Logic and Bit Operations library into your model.

  2. Double-click the block to open the Block Parameters dialog.

  3. Select the type of Operator. In this example, select AND.

  4. In order to perform Bitwise operations with a bit-mask, select Use bit mask.

      Note   If another input uses Bitwise operations, clear the Use bit mask parameter and enter the number of input ports.

  5. In the Bit Mask field, enter a decimal number. Use bin2dec or hex2dec to convert from binary or hexadecimal. In this example, enter hex2dec('D9').

  6. Name the blocks, as shown in, model ex_bit_logic_SL.

  7. Connect the blocks and name the signals, as shown in, model ex_bit_logic_SL.

  8. Press Ctrl+B to build the model and generate code.

Results.  Code implementing the logical operator OR is in the ex_bit_logic_SL_step function in ex_bit_logic_SL.c:

uint8_T u1;
uint8_T y1;
   
void ex_bit_logic_SL_step(void)
{
  y1 = (uint8_T)(u1 & 217);
}

Stateflow Chart

ex_bit_logic_SF/Bit_Logic Stateflow Chart

Procedure.  

  1. Follow the steps for Set Up an Example Model With a Stateflow Chart. This example contains one Inport block and one Outport block.

  2. Name the example model ex_bit_logic_SF.

  3. From the Stateflow Editor, selectTools > Explore to open the Model Explorer.

  4. In the Model Explorer, on the right pane, select Enable C-bit operations.

  5. In the Stateflow Editor, create the Stateflow diagram, ex_bit_logic_SF/Bit_Logic.

  6. Press Ctrl+B to build the model and generate code.

Results.  Code implementing the logical operator OR is in the ex_bit_logic_SF_step function in ex_bit_logic_SF.c:

uint8_T u1;
uint8_T y1;

void bit_logic_SF_step(void)
{
   y1 = (uint8_T)(u1 & 0xD9);
}

MATLAB Function Block

In this example, to demonstrate theMATLAB Function block method for implementing bitwise logic into the generated code, use the bitwise OR, '|'.

Procedure.  

  1. Follow the steps for Set Up an Example Model With a MATLAB Function Block. This example model contains two Inport blocks and one Outport block.

  2. Name your model ex_bit_logic_ML.

  3. In the MATLAB Function Block Editor enter the function, as follows:

    function y1 = fcn(u1, u2)
    
    y1 = bitor(u1, u2);
    end
  4. Press Ctrl+B to build the model and generate code.

Results.  Code implementing the bitwise operator OR is in the ex_bit_logic_ML_step function in ex_bit_logic_ML.c:

uint8_T u1;
uint8_T u2;
uint8_T y1;

void ex_bit_logic_ML_step(void)
{
 y1 = (uint8_T)(u1 | u2);
}
  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS