Main Content

Configure 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 (Simulink Coder).

See Also

| | |

Related Topics