Structures of Parameters

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

C Construct

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

myStructType myStruct = {
  2.0,
  -2.0
} ; 

Procedure

1. Open the example model rtwdemo_paraminline.

2. On the Modeling tab, click Model Data Editor. In the Model Data Editor, select the Parameters tab.

3. 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.

4. Set the value of the Gain parameter in the G2 block to myStruct.G2.

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

6. 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.

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

8. 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.

9. Add a field G2 whose value is -2, and then close the Variable Editor.

10. In the Simulink.Parameter property dialog box, on the Code Generation tab, set Storage class to ExportedGlobal. The structure myStruct appears in the generated code as a global variable.

11. Generate code from the model.

Results

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

typedef struct {
  real_T G1;
  real_T G2;
} struct_6h72eH5WFuEIyQr5YrdGuB;

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

/* Exported block parameters */
struct_6h72eH5WFuEIyQr5YrdGuB myStruct = {
  2.0,
  -2.0
} ;                                    /* Variable: myStruct
                                        * Referenced by:
                                        *   '<Root>/G1'
                                        *   '<Root>/G2'
                                        */

Specify Name of Structure Type

1. 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.

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

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

4. 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.

5. Generate code from the model.

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

myStructType myStruct = {
  2.0,
  -2.0
} ;                                    /* Variable: myStruct

Related Topics