Documentation Center

  • Trial Software
  • Product Updates

Custom Data Types

Custom Data Types in C S-Functions

C S-Functions can communicate using user-defined data types. There are two broad categories for these data types:

  • Simulink® recognizable custom data types — These are custom data types from a Simulink.AliasType, Simulink.Bus, Simulink.NumericType, or an Enumerated data type that can also interact with other Simulink blocks.

  • Opaque data types — These are data types for use only with S-Function blocks programmed to understand them. You might define opaque data types in cases in which other Simulink blocks do not need to use the data types.

Using Simulink Recognizable Data Types in C S-Functions

To register a custom data type recognizable by Simulink, the S-function mdlInitializeSizes routine must 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 Opaque Data Types in C S-Functions

For cases in which S-Functions need to communicate using a data type that cannot be understood by Simulink, the S-function 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.

Define the user-defined data type in an external header file to include in the level 2 C S-Function.

/* Define the structure of the user-defined data type */
typedef struct{
    int8_T   a;
    uint16_T b;
}myStruct;

Place the following code at the beginning of mdlInitializeSizes to set the size and zero representation of the custom data type myStruct.

/* Define variables */
int_T   status;
DTypeId  id;

myStruct tmp;

/* Register the user-defined data types */
id = ssRegisterDataType(S, "myStruct");
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);

    Note   If you have Simulink Coder™, you cannot use the software to generate code for S-functions that contain macros to define custom data types. You must use an inline S-function that accesses Target Language Compiler functions to generate code with custom data types. For more information, see Inlining S-Functions.

Using Custom Data Types in Level-2 MATLAB S-Functions

Level-2 MATLAB 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 MATLAB 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 MATLAB S-function, using one of the following three methods:

    Note   If the registered data type is not one of the Simulink built-in data types, you must have a Fixed-Point Designer™ license.

If you have Fixed-Point Designer, inspect the example models and S-functions provided with the software for examples using the macros for defining fixed-point data types.

Was this topic helpful?