| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Real-Time Workshop |
| Contents | Index |
| Learn more about Real-Time Workshop |
For an example of the code generated from Simulink parameters with different data types, run the demo model rtwdemo_paramdt. This demo model shows options that are available for controlling the data type of tunable parameters in the generated code. The model's subsystem includes several instances of Gain blocks feeding Saturation blocks. Each pair of blocks uses a workspace variable of a particular data type, as shown in the next figure.

The Simulink engine initializes the parameters in the demo model by executing the script rtwdemo_paramdt_data.m. You can view the initialization script and inspect the workspace variables in Model Explorer by double-clicking the appropriate yellow boxes in the demo model.
In the demo model, note that the Inline parameters option on the Optimization pane of the Configuration Parameters dialog box is selected. The Model Parameter Configuration dialog box reveals that all base workspace variables (with the exception of Kinline) have their Storage class property set to ExportedGlobal. Consequently, Kinline is a nontunable parameter while the remaining variables are tunable parameters.
To generate code for the demo model, double-click the blue boxes. The following table shows both the MATLAB code used to initialize parameters and the code generated for each parameter in the rtwdemo_paramdt model.
Parameter & MATLAB Code | Generated Variable Declaration and Code |
|---|---|
Kinline Kinline = 2; | rtb_Gain1 = rtwdemo_paramdt_U.In1 * 2.0F; . . rtwdemo_paramdt_Y.Out1 = rt_SATURATE(rtb_Gain1, 0.0F, 2.0F); |
Kcs Kcs = 3; | real32_T Kcs = 3.0F; . . rtb_Gain1 = rtwdemo_paramdt_U.In2 * Kcs; . . rtwdemo_paramdt_Y.Out2 = rt_SATURATE(rtb_Gain1, 0.0F, Kcs); |
Ksingle Ksingle = single(4); | real32_T Ksingle = 4.0F; . . rtb_Gain1 = rtwdemo_paramdt_U.In3 * Ksingle; . . rtwdemo_paramdt_Y.Out3 = rt_SATURATE(rtb_Gain1, 0.0F, Ksingle); |
Kint8 Kint8 = int8(5); | int8_T Kint8 = 5; . . rtb_Gain1 = rtwdemo_paramdt_U.In4 * ((real32_T)( Kint8 )); . . rtwdemo_paramdt_Y.Out4 = rt_SATURATE(rtb_Gain1, 0.0F, ((real32_T)( Kint8 ))); |
Kfixpt Kfixpt = Simulink.Parameter; Kfixpt.Value = 6; Kfixpt.DataType = ... 'fixdt(true, 16, 2^-5, 0)'; Kfixpt.RTWInfo.StorageClass = ... 'ExportedGlobal'; | int16_T Kfixpt = 192; . . rtb_Gain1 = rtwdemo_paramdt_U.In5 * (((real32_T)ldexp((real_T)Kfixpt, -5))); . . rtwdemo_paramdt_Y.Out5 = rt_SATURATE(rtb_Gain1, 0.0F, (((real32_T)ldexp((real_T)Kfixpt, -5)))); |
Kalias aliasType = ...
Simulink.AliasType('single');
Kalias = Simulink.Parameter;
Kalias.Value = 7;
Kalias.DataType = 'aliasType';
Kalias.RTWInfo.StorageClass = ...
'ExportedGlobal';
| typedef real32_T aliasType; . . aliasType Kalias = 7.0F; . . rtb_Gain1 = rtwdemo_paramdt_U.In6 * Kalias; . . rtwdemo_paramdt_Y.Out6 = rt_SATURATE(rtb_Gain1, 0.0F, Kalias); |
Kuser userType = Simulink.NumericType; userType.DataTypeMode = ... 'Fixed-point: slope and bias scaling'; userType.Slope = 2^-3; userType.isAlias = true; Kuser = Simulink.Parameter; Kuser.Value = 8; Kuser.DataType = 'userType'; Kuser.RTWInfo.StorageClass = ... 'ExportedGlobal'; | typedef int16_T userType; . . userType Kuser = 64; . . rtb_Gain1 = rtwdemo_paramdt_U.In7 * (((real32_T)ldexp((real_T)Kuser, -3))); . . rtwdemo_paramdt_Y.Out7 = rt_SATURATE(rtb_Gain1, 0.0F, (((real32_T)ldexp((real_T)Kuser, -3)))); |
The salient features of the code generated for this demo model are as follows:
The Real-Time Workshop product inlines nontunable parameters, for example, Kinline. However, the product does not inline tunable parameters, such as Kcs, Ksingle, and Kint8.
The Simulink engine treats tunable parameters of data type double in a context-sensitive manner, such that the parameter inherits its data type from the context in which the block uses it. For example, Kcs inherits a single data type from the Gain block's input signal.
If a parameter's data type matches that of the block's run-time parameter, the block can use the tunable parameter without any transformation. Consequently, the Real-Time Workshop product need not cast the parameter from one data type to another, as illustrated by Ksingle and Kalias. However, if a parameter's data type does not match that of the block's run-time parameter, the block cannot readily compute its output. In this case, the product casts parameters to the appropriate data type. For example, Kint8, Kfixpt, and Kuser require casts to a single data type for compatibility with the input signals to the Gain and Saturation blocks.
If you are using an ERT target and a parameter specifies a data type alias, for example, created by an instance of the Simulink.AliasType class, its variable definition in the generated code uses the alias data type. For example, the Real-Time Workshop product declares Kalias and Kuser to be of data types aliasType and userType, respectively.
If a parameter specifies a fixed-point data type, the Real-Time Workshop product initializes its value in the generated code to the value of Q computed from the expression V = SQ + B (see the Simulink Fixed Point documentation for more information about fixed-point semantics and notation), where
V is a real-world value
Q is an integer that encodes V
S is the slope
B is the bias
For example, Kfixpt has a real-world value of 6, slope of 2-5, and bias of 0. Consequently, the product declares the value of Kfixpt to be 192.
![]() | Parameter Configuration Quick Reference Diagram | Tunable Workspace Parameter Data Type Considerations | ![]() |

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