Main Content

Type Qualifiers

This example shows how to apply the const and volatile keywords to a global variable that represents parameter data.

C Construct

const volatile double myParam = 9.8;

Procedure

1. Open the example model ex_const_volatile.

open_system('ex_const_volatile');

2. In the model, select the Gain block. In the Property Inspector, set the value of the Gain parameter to myParam.

3. Next to the parameter value, click the action button (the button with three vertical dots) and select Create.

myParam = Simulink.Parameter(9.8);
set_param('ex_const_volatile/Gain','Gain','myParam');

4. In the Create New Data dialog box, set Value to Simulink.Parameter(9.8). Click Create. A Simulink.Parameter object, myParam, appears in the base workspace. The Gain block uses the object to set the value of the Gain parameter, in this case, 9.8.

5. On the Code Generation tab, click the Configure in Coder App button. In the Code Mappings editor, set Storage Class to ConstVolatile. Alternatively, to apply only one of the keywords, use the storage classes Const or Volatile.

myParam.StorageClass = 'ConstVolatile';

6. To build the model and generate code, press Ctrl+B .

currentDir = pwd;
[~,cgDir] = rtwdemodir();
evalc('rtwbuild(''ex_const_volatile'')');

Results

The generated source file ex_const_volatile.c defines myParam by using the const and volatile keywords.

file = fullfile('ex_const_volatile_ert_rtw','ex_const_volatile.c');
rtwdemodbtype(file,'/* Definition for custom', ...
    'const volatile real_T myParam = 9.8;',1,1);
/* Definition for custom storage class: ConstVolatile */
const volatile real_T myParam = 9.8;   /* Referenced by: '<Root>/Gain' */

Related Topics