Main Content

Control Data Types and Generate Code withMATLAB FunctionBlock

Use can use theMATLAB Functionblock 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 aMATLAB Functionblock generates efficient C/C++ code. This code meets the strict memory and data type requirements of embedded target environments.

Data Type Override withMATLAB FunctionBlock

When you use theMATLAB Functionblock 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 Functionblock 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, seeFixed-Point Instrumentation and Data Type Override.

Input Signal or Parameter Type Data Type Override Setting Data Type Override Applies To Setting Override Data Type
Inheritedsingle Double All numeric typesorFloating-point Built-indouble
Single All numeric typesorFloating-point Built-insingle
Scaled double All numeric typesorFloating-point fi扩大两倍
Specifiedsingle Double All numeric typesorFloating-point Built-indouble
Single All numeric typesorFloating-point Built-insingle
Scaled double All numeric typesorFloating-point fi扩大两倍
Inheriteddouble Double All numeric typesorFloating-point Built-indouble
Single All numeric typesorFloating-point Built-insingle
Scaled double All numeric typesorFloating-point fi扩大两倍
Specifieddouble Double All numeric typesorFloating-point Built-indouble
Single All numeric typesorFloating-point Built-insingle
Scaled double All numeric typesorFloating-point fi扩大两倍
InheritedFixed Double All numeric typesorFixed-point fi double
Single All numeric typesorFixed-point fi single
Scaled double All numeric typesorFixed-point fi扩大两倍
SpecifiedFixed Double All numeric typesorFixed-point fi double
Single All numeric typesorFixed-point fi single
Scaled double All numeric typesorFixed-point fi扩大两倍

Fixed-Point Data Types withMATLAB FunctionBlock

Code generation from MATLAB supports a significant number of Fixed-Point Designer™ functions. For information about which Fixed-Point Designer functions are supported, seeFunctions 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 aMATLAB Functionblock in a fixed-point model. For more information, seeSpecify MATLAB Function Block Properties.

  1. Create a new model. Place aMATLAB Functionblock in the model.

  2. Open the Model Explorer. On theModelingtab, in theDesignsection, clickModel Explorer.

  3. Expand theuntitled*node in theModel Hierarchypane. Then, select theMATLAB Functionnode.

These parameters apply toMATLAB Functionblocks in models that use fixed-point and integer data types:

  • Treat these inherited Simulink signal types as fi objects— Choose whether to treat inherited fixed-point and integer signals asfiobjects.

    • Fixed-point— TheMATLAB Functionblock treats all fixed-point inputs as Fixed-Point Designerfiobjects.

    • Fixed-point & Integer— TheMATLAB Functionblock treats all fixed-point and integer inputs as Fixed-Point Designerfiobjects.

  • MATLAB Function fimath— Specify thefimathproperties for the block to associate with these objects:

    • All fixed-point and integer input signals to theMATLAB Functionblock that you choose to treat asfiobjects.

    • Allfiandfimathobjects constructed in theMATLAB Functionblock.

    Select one of these options:

    • Same as MATLAB— The block uses the samefimathproperties as the current defaultfimath. The edit box displays the current defaultfimathin read-only form.

    • Specify Other— Specify your ownfimathobject in the edit box.

UsefimathObjects inMATLAB FunctionBlocks

Open theProperty Inspectorpane for theMATLAB Functionblock. On theModelingtab, in theDesignsection, selectProperty Inspector. In theProperty Inspectorpane, expandFixed-point properties.

TheMATLAB Function block fimathparameter enables you to specify one set offimathobject properties for theMATLAB Functionblock. The block associates thefimathproperties you specify with the following objects:

  • All fixed-point and integer input signals to theMATLAB Functionblock that you choose to treat asfiobjects.

  • Allfiandfimathobjects constructed in theMATLAB Functionblock.

You can select one of the following options:

  • Same as MATLAB— The block uses the samefimathproperties as the current defaultfimath. The edit box displays the current defaultfimathin read-only form.

  • Specify Other— Specify your ownfimathobject in the edit box. You can do this in two ways:

    • Construct thefimathobject inside the edit box.

    • Construct thefimathobject in the MATLAB or model workspace and then enter its variable name in the edit box.

      Note

      If you use this option and plan to share your model with others, make sure you define the variable in the model workspace.

For an example showing theMATLAB Function fimathoptions work, seeSpecify Fixed-Point Math Properties in MATLAB Function Block.

The Fixed-Point Designerisfimathlocalfunction supports code generation for MATLAB.

分享模型包含Fixed-PointMATLAB FunctionBlocks

To share a fixed-point model containing aMATLAB Functionblock, you must first move any variables you define in the MATLAB workspace, includingfimathobjects, to the model workspace. For example:

  1. Create a new model. Place aMATLAB Functionblock in the model.

  2. Define afimathobject 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 thisfimathfor any Simulink fixed-point signal entering theMATLAB Functionblock as an input.

  3. Open the Model Explorer.

  4. Expand theuntitled*node in theModel Hierarchypane of the Model Explorer. Select theMATLAB Functionnode.

  5. ForMATLAB Function block fimath, selectSpecify other. In the edit box, enter the variableF. ClickApplyto save your changes.

    You have now defined thefimathproperties to be associated with all Simulink fixed-point input signals and allfiandfimathobjects constructed within the block.

  6. In theModel Hierarchypane, selectBase Workspace. You can see the variableFthat you have defined in the MATLAB workspace listed in theContentspane. 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 variableFfrom the base workspace and paste it into the model workspace listed under the node for your model, in this case,untitled*. 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 Coder)

Related Topics