Products & Services Industries Academia Support User Community Company

Learn more about Simulink   

Custom Data Types

Using Custom Data Types in C S-Functions

To use a user-defined data type, the S-function's mdlInitializeSizes routine must:

  1. Register the data type, using ssRegisterDataType.

  2. Specify the amount of memory in bytes required to store an instance of the data type, using ssSetDataTypeSize.

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

  1. Define an integer pointer to hold the data type identifier for the custom data type.

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

Using Custom Data Types in Level-2 M-File S-Functions

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:

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.

  


Related Products & Applications

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