Control Data Types and Generate Code with MATLAB Function Block - MATLAB & Simulink (original) (raw)

Main Content

You can use the MATLAB Function block to compose a MATLAB® language function in a Simulink® model that generates embeddable code. When you simulate the model or generate code for a target environment, a function in a MATLAB Function block generates efficient C/C++ code. This code meets the strict memory and data type requirements of embedded target environments.

Data Type Override with MATLAB Function Block

When you use the MATLAB Function block in a Simulink model that specifies data type override, the block determines the data type override equivalents of the input signal and parameter types. The block then uses these equivalent values to run the simulation. This table shows how theMATLAB Function block determines the data type override equivalent using the data type of the input signal or parameter and the data type override settings in the Simulink model. For more information about data type override, see Fixed-Point Instrumentation and Data Type Override.

Input Signal or Parameter Type Data Type Override Setting Data Type Override Applies To Setting Override Data Type
Inherited single Double All numeric types orFloating-point Built-in double
Single All numeric types orFloating-point Built-in single
Scaled double All numeric types orFloating-point fi scaled double
Specified single Double All numeric types orFloating-point Built-in double
Single All numeric types orFloating-point Built-in single
Scaled double All numeric types orFloating-point fi scaled double
Inherited double Double All numeric types orFloating-point Built-in double
Single All numeric types orFloating-point Built-in single
Scaled double All numeric types orFloating-point fi scaled double
Specified double Double All numeric types orFloating-point Built-in double
Single All numeric types orFloating-point Built-in single
Scaled double All numeric types orFloating-point fi scaled double
Inherited Fixed Double All numeric types orFixed-point fi double
Single All numeric types orFixed-point fi single
Scaled double All numeric types orFixed-point fi scaled double
Specified Fixed Double All numeric types orFixed-point fi double
Single All numeric types orFixed-point fi single
Scaled double All numeric types orFixed-point fi scaled double

Fixed-Point Data Types with MATLAB Function Block

Code generation from MATLAB supports a significant number of Fixed-Point Designer™ functions. For information about which Fixed-Point Designer functions are supported, see Functions Supported for Code Acceleration or C Code Generation. To simulate models using fixed-point data types in Simulink, you must have a Fixed-Point Designer license.

Specify Fixed-Point Parameters in the Model Explorer

You can use the Model Explorer to specify parameters for a MATLAB Function block in a fixed-point model.

  1. Create a new model. Place a MATLAB Function block in the model.
  2. Open the Model Explorer. On the Modeling tab, in the Design section, click Model Explorer.
  3. Expand the node in the pane. Then, select the node.

These parameters apply to MATLAB Function blocks in models that use fixed-point and integer data types:

Use fimath Objects in MATLAB Function Blocks

Open the Property Inspector pane for the MATLAB Function block. On the Modeling tab, in theDesign section, select Property Inspector. In the Property Inspector pane, expand Fixed-point properties.

The MATLAB Function block fimath parameter enables you to specify one set of fimath object properties for theMATLAB Function block. The block associates thefimath properties you specify with the following objects:

You can select one of the following options:

For an example showing the MATLAB Function fimath options work, see Specify Fixed-Point Math Properties in MATLAB Function Block.

The Fixed-Point Designerisfimathlocal function supports code generation for MATLAB.

Share Models Containing Fixed-Point MATLAB Function Blocks

To share a fixed-point model containing a MATLAB Function block, you must first move any variables you define in the MATLAB workspace, including fimath objects, to the model workspace. For example:

  1. Create a new model. Place a MATLAB Function block in the model.
  2. Define a fimath object in the MATLAB workspace.
    F = fimath('RoundingMethod','Floor','OverflowAction','Wrap',...
    'ProductMode','KeepLSB','ProductWordLength',32,...
    'SumMode','KeepLSB','SumWordLength',32)
    F =
    RoundingMethod: Floor
    OverflowAction: Wrap
    ProductMode: KeepLSB
    ProductWordLength: 32
    SumMode: KeepLSB
    SumWordLength: 32
    CastBeforeSum: true

You can use thisfimath for any Simulink fixed-point signal entering the MATLAB Function block as an input. 3. Open the Model Explorer. 4. Expand the node in the pane of the Model Explorer. Select the node. 5. For MATLAB Function block fimath, selectSpecify other. In the edit box, enter the variableF. Click Apply to save your changes.
You have now defined the fimath properties to be associated with all Simulink fixed-point input signals and all fi andfimath objects constructed within the block. 6. In the pane, select . You can see the variableF that you have defined in the MATLAB workspace listed in the pane. If you send this model to another user, that user must first define that same variable in the MATLAB workspace to get the same results. 7. Cut the variable F from the base workspace and paste it into the model workspace listed under the node for your model, in this case, . The Model Explorer now appears as shown.

You can share your model with another user. Because you included the required variables in the workspace of the model itself, another user can run the model and get the correct results. Receiving and running the model does not require any extra steps.

See Also

MATLAB Function | Overview of Code Generation Using MATLAB Coder (MATLAB Coder)

Topics