MATLAB Examples

Structures of Parameters

Create a structure in the generated code. The structure stores parameter data.

Contents

C Construct

typedef struct {
  double G1;
  double G2;
} myStructType;

myStructType myStruct = {
  2.0,
  -2.0
} ; 

Procedure

Open the example model rtwdemo_paraminline.

rtwdemo_paraminline

Select View > Model Data.

In the Model Data Editor, select the Parameters tab.

In the model, click the Gain block labeled G1.

In the Model Data Editor, use the Value column to set the value of the Gain parameter to myStruct.G1.

Use the Model Data Editor to set the value of the Gain parameter in the G2 block to myStruct.G2.

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

In the Create New Data dialog box, set Value to Simulink.Parameter(struct) and click Create. A Simulink.Parameter object named myStruct appears in the base workspace.

In the Simulink.Parameter property dialog box, next to the Value property, click the action button and select Open Variable Editor.

In the Variable Editor, right-click the white space under the Field column and select New. Name the new structure field G1. Use the Value column to set the value of the field to 2.

Use the Variable Editor to add a field G2 whose value is -2 and then close the Variable Editor.

In the Simulink.Parameter property dialog box, set Storage class to ExportedGlobal. With this setting, the structure myStruct appears in the generated code as a global variable.

Alternatively, use these commands to configure the blocks and create myStruct.

rtwdemo_paraminline
set_param('rtwdemo_paraminline/G1','Gain','myStruct.G1')
set_param('rtwdemo_paraminline/G2','Gain','myStruct.G2')

myStruct.G1 = 2;
myStruct.G2 = -2;
myStruct = Simulink.Parameter(myStruct);
myStruct.CoderInfo.StorageClass = 'ExportedGlobal';

Results

Generate code from the model.

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

The generated header file rtwdemo_paraminline_types.h defines a structure type with a random name.

file = fullfile('rtwdemo_paraminline_grt_rtw',...
    'rtwdemo_paraminline_types.h');
rtwdemodbtype(file,'typedef struct {','} struct_6h72eH5WFuEIyQr5YrdGuB;',...
    1,1)
typedef struct {
  real_T G1;
  real_T G2;
} struct_6h72eH5WFuEIyQr5YrdGuB;

The source file rtwdemo_paraminline.c defines and initializes the structure variable myStruct.

file = fullfile('rtwdemo_paraminline_grt_rtw','rtwdemo_paraminline.c');
rtwdemodbtype(file,'struct_6h72eH5WFuEIyQr5YrdGuB myStruct',...
    '/* Variable: myStruct',1,1)
struct_6h72eH5WFuEIyQr5YrdGuB myStruct = {
  2.0,
  -2.0
} ;                                    /* Variable: myStruct

Specify Name of Structure Type

Optionally, specify a name to use for the structure type definition (struct).

At the command prompt, use the function Simulink.Bus.createObject to create a Simulink.Bus object that represents the structure type.

Simulink.Bus.createObject(myStruct.Value);

The default name of the object is slBus1. Change the name by copying the object into a new MATLAB variable.

myStructType = copy(slBus1);

In the Model Data Editor, click the Show/refresh additional information button.

In the data table, find the row that corresponds to myStruct. Use the Data Type column to set the data type of myStruct to Bus: myStructType.

Alternatively, at the command prompt, use this command to set the data type of myStruct.

myStruct.DataType = 'Bus: myStructType';

Generate code from the model.

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

The code generates the definition of the structure type myStructType and uses this type to define the global variable myStruct.

rtwdemodbtype(file,'myStructType myStruct = {','/* Variable: myStruct',...
    1,1)
myStructType myStruct = {
  2.0,
  -2.0
} ;                                    /* Variable: myStruct