Products & Services Solutions Academia Support User Community Company

Learn more about Real-Time Workshop Embedded Coder   

Selecting User Data Types for Signals and Parameters

Preparing User Data Types

You can use user-defined data types for Simulink signals and parameters and their corresponding identifiers in generated code. This is true whether or not a signal or parameter has a Simulink data object associated with it.

Before you can select a user data type for a signal or parameter, you must:

  1. Create a user data type (alias), as explained in the description of Simulink.AliasType in the Simulink documentation. For the example in Selecting the User Data Types demonstrating how to select user data types for signals and parameters, create the alias data type f32.

  2. Register the user data type so that it is associated with the corresponding MathWorks C/C++ data type, as explained in Creating User Data Types. For the example, register the data type f32 so that it is associated with type real32_T. The call to function addUserDataType in the sl_customization.m file you use for the registration must specify:

    • f32 as the user data type

    • real32_T as the built-in data type

    • <userdata_types.h> as the user header file that is to include the user data type definition

    For example,

    function sl_customization(cm)
    
    hObj = cm.slDataObjectCustomizer;
    
    addUserDataType(hObj, 'f32', 'real32_T', '<userdata_types.h>');
    
    end
  3. If you have not already done so, add the directory containing the sl_customization.m file that you created or modified in step 1 to the MATLAB search path.

  4. Open a model. The example uses the following model.

  5. Create a data dictionary for the model, as explained in Creating Simulink and mpt Data Objects, to associate signals and parameters with data objects. For the example, the Model Explorer display must appear as shown below. The three data objects that appear, sig1 , sig2, and g, and the registered user data type, f32, appear in the middle pane. The "T" indicates that f32 is an alias data type.

For the selection procedure and to continue with the example, continue to Selecting the User Data Types.

Selecting the User Data Types

After completing the preparation explained in Preparing User Data Types, you can use the user-defined data types for Simulink signals and parameters and for their corresponding identifiers in the generated code. You can use user-defined data types with signals and parameters whether or not they have Simulink objects associated with them.

  1. For an mpt object that is associated with a signal or parameter in your model, in the Data type field, select the user data type that you want. For example, select f32, for sig1.

    This selects f32 for the sig1 data object in the data dictionary, but does not select f32 for the corresponding labeled signal in the model. Therefore, the two may be in conflict. If you try to update the model, you could get an error message.

    To continue with the example, type f32 into the Data type field for sig1.

  2. Select the model and double-click the signal or parameter source block. (The source block of a model signal or parameter controls the signal's or parameter's data type.) For example, in the example model the Sum block is the source block for sig1. Double-click the Sum block.

    The Function Block Parameters dialog box opens.

  3. Select the Signal Attributes tab.

  4. In the Output data type or Parameter data type field, type the name of the user data type that you specified for the data object in step 1, and click Apply. The user data type of the signal in the model and that of the signal object are now the same.

    Alternatively, you can use dictionary-driven data typing. In the Output data type field, specify object.DataType, where object is the case-sensitive object name. For example, you can specify sig1.DataType instead of f32.

    The advantage of using the alternative is that subsequent user data type changes for the object in the dictionary automatically change the user data type of the corresponding model signal or parameter.

  5. Repeat steps 1 through 4 for each remaining model signal and parameter that has a corresponding signal object for which you selected a user data type.

  6. Save the model and save all of the data objects in the MATLAB base workspace in a .mat file for reuse later. Generated code for sig1 in the example model (with default MPF settings) would appear as follows:

    In sampleUserDT.cf32 sig1;
    In sampleUserDT_types.h#include <userdata_types.h>

  


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