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 types orFloating-point |
Built-indouble |
Single |
All numeric types orFloating-point |
Built-insingle |
|
Scaled double |
All numeric types orFloating-point |
fi扩大两倍 |
|
Specifiedsingle |
Double |
All numeric types orFloating-point |
Built-indouble |
Single |
All numeric types orFloating-point |
Built-insingle |
|
Scaled double |
All numeric types orFloating-point |
fi扩大两倍 |
|
Inheriteddouble |
Double |
All numeric types orFloating-point |
Built-indouble |
Single |
All numeric types orFloating-point |
Built-insingle |
|
Scaled double |
All numeric types orFloating-point |
fi扩大两倍 |
|
Specifieddouble |
Double |
All numeric types orFloating-point |
Built-indouble |
Single |
All numeric types orFloating-point |
Built-insingle |
|
Scaled double |
All numeric types orFloating-point |
fi扩大两倍 |
|
InheritedFixed |
Double |
All numeric types orFixed-point |
fi double |
Single |
All numeric types orFixed-point |
fi single |
|
Scaled double |
All numeric types orFixed-point |
fi扩大两倍 |
|
SpecifiedFixed |
Double |
All numeric types orFixed-point |
fi double |
Single |
All numeric types orFixed-point |
fi single |
|
Scaled double |
All numeric types orFixed-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.
Create a new model. Place aMATLAB Functionblock in the model.
Open the Model Explorer. On theModelingtab, in theDesignsection, clickModel Explorer.
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 as
fi
objects.Fixed-point
— TheMATLAB Functionblock treats all fixed-point inputs as Fixed-Point Designerfi
objects.Fixed-point & Integer
— TheMATLAB Functionblock treats all fixed-point and integer inputs as Fixed-Point Designerfi
objects.
MATLAB Function fimath— Specify the
fimath
properties for the block to associate with these objects:All fixed-point and integer input signals to theMATLAB Functionblock that you choose to treat as
fi
objects.All
fi
andfimath
objects constructed in theMATLAB Functionblock.
Select one of these options:
Same as MATLAB— The block uses the same
fimath
properties as the current defaultfimath
. The edit box displays the current defaultfimath
in read-only form.Specify Other— Specify your own
fimath
object in the edit box.
Usefimath
Objects 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 offimath
object properties for theMATLAB Functionblock. The block associates thefimath
properties you specify with the following objects:
All fixed-point and integer input signals to theMATLAB Functionblock that you choose to treat as
fi
objects.All
fi
andfimath
objects constructed in theMATLAB Functionblock.
You can select one of the following options:
Same as MATLAB— The block uses the same
fimath
properties as the current defaultfimath
. The edit box displays the current defaultfimath
in read-only form.Specify Other— Specify your own
fimath
object in the edit box. You can do this in two ways:Construct the
fimath
object inside the edit box.Construct the
fimath
object 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 Designerisfimathlocal
function 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, includingfimath
objects, to the model workspace. For example:
Create a new model. Place aMATLAB Functionblock in the model.
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
fimath
for any Simulink fixed-point signal entering theMATLAB Functionblock as an input.Open the Model Explorer.
Expand theuntitled*node in theModel Hierarchypane of the Model Explorer. Select theMATLAB Functionnode.
ForMATLAB Function block fimath, selectSpecify other. In the edit box, enter the variable
F
. ClickApplyto save your changes.You have now defined the
fimath
properties to be associated with all Simulink fixed-point input signals and allfi
andfimath
objects constructed within the block.In theModel Hierarchypane, selectBase Workspace. You can see the variable
F
that 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.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,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 Function|Code Generation Workflow(MATLAB Coder)