MATLAB Examples

Represent Block Parameter as Tunable Global Variable

This example shows how to create tunable parameter data by representing block parameters as global variables in the generated code.


Configure Block Parameter by Using Parameter Object

Open the example model rtwdemo_paraminline and configure it to show the generated names of blocks.


In the model, select View > Model Data.

In the Model Data Editor, inspect the Parameters tab.

In the model, click the G1 Gain block. The Model Data Editor highlights the row that corresponds to the Gain parameter of the block.

In the Model Data Editor Value column, change the gain value from 2 to myGainParam.

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

In the Create New Data block dialog box, set Value to Simulink.Parameter(2). Click Create. A Simulink.Parameter object myGainParam stores the parameter value, 2, in the base workspace.

In the myGainParam dialog box, set Storage class to ExportedGlobal and click OK. This storage class causes the parameter object to appear in the generated code as a tunable global variable.

Alternatively, to create the parameter object and configure the model, use these commands at the command prompt:

myGainParam = Simulink.Parameter(2);
myGainParam.CoderInfo.StorageClass = 'ExportedGlobal';

Use the Model Data Editor to create a parameter object, myOtherGain, for the G2 Gain block. Apply the storage class ExportedGlobal.

Alternatively, use these commands at the command prompt:

myOtherGain = Simulink.Parameter(-2);
myOtherGain.CoderInfo.StorageClass = 'ExportedGlobal';

Generate and Inspect Code

Generate code from the model.

### Starting build procedure for model: rtwdemo_paraminline
### Successful completion of build procedure for model: rtwdemo_paraminline

The generated file rtwdemo_paraminline.h contains extern declarations of the global variables myGainParam and myOtherGain. You can include (#include) this header file so that your code can read and write the value of the variable during execution.

file = fullfile('rtwdemo_paraminline_grt_rtw','rtwdemo_paraminline.h');
rtwdemodbtype(file,'extern real_T myGainParam;','Referenced by: ''<Root>/G2''',1,1)
extern real_T myGainParam;             /* Variable: myGainParam
                                        * Referenced by: '<Root>/G1'
extern real_T myOtherGain;             /* Variable: myOtherGain
                                        * Referenced by: '<Root>/G2'

The file rtwdemo_paraminline.c allocates memory for and initializes myGainParam and myOtherGain.

file = fullfile('rtwdemo_paraminline_grt_rtw','rtwdemo_paraminline.c');
rtwdemodbtype(file,'/* Exported block parameters */','Referenced by: ''<Root>/G2''',1,1)
/* Exported block parameters */
real_T myGainParam = 2.0;              /* Variable: myGainParam
                                        * Referenced by: '<Root>/G1'
real_T myOtherGain = -2.0;             /* Variable: myOtherGain
                                        * Referenced by: '<Root>/G2'

The generated code algorithm in the model step function uses myGainParam and myOtherGain for calculations.

rtwdemodbtype(file,'/* Model step function */','/* Model initialize function */',1,0)
/* Model step function */
void rtwdemo_paraminline_step(void)
  /* Outport: '<Root>/Out1' incorporates:
   *  Gain: '<Root>/G1'
   *  Gain: '<Root>/G2'
   *  Inport: '<Root>/In1'
   *  Sum: '<Root>/Sum'
  rtwdemo_paraminline_Y.Out1 = myGainParam * rtwdemo_paraminline_U.In1 +
    myOtherGain * -75.0;