| Products & Services | Industries | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Simulink |
| Contents | Index |
| Learn more about Simulink |
| On this page… |
|---|
To use a user-defined data type, the S-function's mdlInitializeSizes routine must:
Register the data type, using ssRegisterDataType.
Specify the amount of memory in bytes required to store an instance of the data type, using ssSetDataTypeSize.
Specify the value that represents zero for the data type, using ssSetDataTypeZero.
The following code placed at the beginning of mdlInitializeSizes sets the size and zero representation of a custom data type named myDataType.
/* Define variables */
int_T status;
DTypeId id;
/* Define the structure of the user-defined data type */
typedef struct{
int8_T a;
uint16_T b;
}myStruct;
myStruct tmp;
/* Register the user-defined data types */
id = ssRegisterDataType(S, "myDataType");
if(id == INVALID_DTYPE_ID) return;
/* Set the size of the user-defined data type */
status = ssSetDataTypeSize(S, id, sizeof(tmp));
if(status == 0) return;
/* Set the zero representation */
tmp.a = 0;
tmp.b = 1;
status = ssSetDataTypeZero(S, id, &tmp);
To register a custom data type from a Simulink.AliastType, Simulink.NumericType, or Simulink.StructType object, the S-function's mdlInitializeSizes routine must:
Define an integer pointer to hold the data type identifier for the custom data type.
Register the data type, using ssRegisterTypeFromNamedObject.
For example, the following code placed at the beginning of mdlInitializeSizes defines a custom data type from a Simulink.AliasType object named u8 in the MATLAB workspace. The example then assigns the custom data type to the first output port.
int id1; ssRegisterTypeFromNamedObject(S, "u8", &id1); ssSetOutputPortDataType(S, 0, id1);
In addition, you can use the identifier id1 to assign this data type to S-function parameters, DWork vectors, and input ports.
Note You cannot use the Real-Time Workshop product to generate code for S-functions that contain macros to define custom data types. You must use an inlined S-function that accesses Target Language Compiler functions to generate code with custom data types. See Inlining S-Functions in the Real-Time Workshop Target Language Compiler documentation.) |
Level-2 M-file S-functions do not support defining custom data types within the S-function. However, input and output ports can inherit their data types from a Simulink.NumericType or Simulink.AliasType. For example, the S-function in the following model inherits its input data type from the Constant block:

The Constant block's Output data type field contains the value MyDouble, which is a Simulink.NumericType defined in the MATLAB workspace with the following lines of code:
MyDouble = Simulink.NumericType; MyDouble.IsAlias = 1;
The input and output ports of the Level-2 M-file S-function msfcn_inheritdt.m inherit their data types. When the Simulink engine performs data type propagation, it assigns the data type MyDouble to these ports.
You can define a fixed-point data type within a Level-2 M-file S-function, using one of the following three methods:
RegisterDataTypeFxpBinaryPoint registers a fixed-point data type with binary point-only scaling
RegisterDataTypeFxpFSlopeFixExpBias registers a fixed-point data type with [Slope Bias] scaling specified in terms of fractional slope, fixed exponent, and bias
RegisterDataTypeFxpSlopeBias registers a data type with [Slope Bias] scaling
Note If the registered data type is not one of the Simulink built-in data types, you must have a Simulink® Fixed Point™ license. |
Inspect the demo models and S-functions provided with the Simulink Fixed Point product for examples using the macros for defining fixed-point data types.
![]() | Input and Output Ports | Sample Times | ![]() |

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 |