| Contents | Index |
| On this page… |
|---|
#define p_1 9.8;
Use a 'Define' Custom Storage Class
Create a model containing a Gain block.

Press Ctrl+E to open the Configuration Parameters dialog box.
In the Configuration Parameter dialog box, on the Optimization > Signals and Parameters pane, select Inline parameters.
Click Apply and OK.
In your model, double-click the Gain block. The Block Parameters dialog box opens.
In the Value field, enter a variable name. In this example, the variable name is p1.
Press Ctrl+H to open the Model Explorer. On the Model Hierarchy pane, select the Base Workspace.
To add an MPT parameter object, in the menu bar, select Add > MPT Parameter. The parameter appears in the Contents of: Base Workspace pane.
Double-click the mpt.Parameter object and change its name to p1.
Click the p1 parameter. The data object parameters are displayed in the right pane of the Model Explorer.
In the Value field, enter 9.8. In the Code generation options section, click the Storage Class drop-down list and select Define(Custom).
Press Ctrl+B to generate code.
Results. The generated code includes the inlined parameter, p1, in ex_define_data_object.c:
/* Model step function */
void ex_define_data_object_step(void)
{
rtY.y1 = p1 * rtU.u1;
}Follow steps 1 through 10 of Use a 'Define' Custom Storage Class.
In the Simulink.Parameter dialog box for p1, in the Value field, enter 9.8. In the Code generation options section, click the Storage Class drop-down list and select ImportFromFile(Custom).
In the Header file parameter, enter the name of the header file, in this example, external_params.h.
Click Apply and OK.
Create the C header file, external_params.h that contains the #define statement:
#ifndef _EXTERNAL_PARAMS #define _EXTERNAL_PARAMS #define p1 9.8 #endif /* EOF */
Press Ctrl+B to generate code.
Results. The generated code includes the inlined parameter, p1, in ex_define_data_object.c:
/* Model step function */
void ex_define_data_object_step(void)
{
ex_define_data_object_Y.Out1 = p1 * ex_define_data_object_U.In1;
}You can generate preprocessor conditional directives in your code by implementing variant blocks (Model Variants block or Variant Subsystem block) in your model. In the generated code, preprocessor conditional directives select a section of code to execute at compile time. To implement variants in your model, see Modeling Variant Systems in the Simulink documentation. To generate code for variants, see Variant Systems.
![]() | Functions | Structures | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |