MATLAB Examples

Optimize Generated Code Using Boolean Data for Logical Signals

Optimize generated code by storing logical signals as Boolean data. When you select the model configuration parameter Implement logic signals as Boolean data (vs. double), blocks that generate logic signals output Boolean signals.

The optimization:

  • Reduces the ROM and RAM consumption.
  • Improves execution speed.

Contents

Example Model

Consider the model rtwdemo_logicalAsBoolean. The outputs of the Relational Operator, Logical Operator and HitCrossing blocks are double, even though they represent logical data.

model = 'rtwdemo_logicalAsBoolean';
open_system(model);

Generate Code

Create a temporary folder (in your system temporary folder) for the build and inspection process.

currentDir = pwd;
[~,cgDir] = rtwdemodir();

Build the model.

rtwbuild(model)
### Starting build procedure for model: rtwdemo_logicalAsBoolean
### Successful completion of build procedure for model: rtwdemo_logicalAsBoolean

View the generated code without the optimization. These lines of code are in rtwdemo_logicalAsBoolean.h.

hfile = fullfile(cgDir,'rtwdemo_logicalAsBoolean_ert_rtw',...
    'rtwdemo_logicalAsBoolean.h');
rtwdemodbtype(hfile,'/* External outputs','/* Parameters (auto storage) */',1,0);
/* External outputs (root outports fed by signals with auto storage) */
typedef struct {
  real_T Out1;                         /* '<Root>/Out1' */
  real_T Out2;                         /* '<Root>/Out2' */
  real_T Out3;                         /* '<Root>/Out3' */
} ExtY_rtwdemo_logicalAsBoolean_T;

Enable Optimization

  1. Open the Configuration Parameters dialog box.
  2. Select the Implement logic signals as Boolean data (vs. double) parameter.

Alternatively, you can use the command-line API to enable the optimization:

set_param(model,'BooleanDataType','on');

Generate Code with Optimization

The generated code stores the logical signal output as Boolean data.

Build the model.

rtwbuild(model)
### Starting build procedure for model: rtwdemo_logicalAsBoolean
### Successful completion of build procedure for model: rtwdemo_logicalAsBoolean

View the generated code with the optimization. These lines of code are in rtwdemo_logicalAsBoolean.h.

rtwdemodbtype(hfile,'/* External outputs','/* Parameters (auto storage) */',1,0);
/* External outputs (root outports fed by signals with auto storage) */
typedef struct {
  boolean_T Out1;                      /* '<Root>/Out1' */
  boolean_T Out2;                      /* '<Root>/Out2' */
  boolean_T Out3;                      /* '<Root>/Out3' */
} ExtY_rtwdemo_logicalAsBoolean_T;

Close the model and code generation report.

bdclose(model)
rtwdemoclean;
cd(currentDir)