This example shows how to replace a function that is used inside
a MATLAB® Function block, with a more efficient implementation.
The following model contains a MATLAB Function block which computes
the sine of the input. Use the Code View to replace the built-in
with a lookup table.
Change directories to the folder where the model is located. At the MATLAB command line, enter:
Copy and save the
ex_mySin.slx file to a
local writable folder and open the model.
function y = my_sin(u) %#codegen y = sin(u);
To open the Fixed-Point Tool, in the Apps tab, expand the Apps gallery and select Fixed-Point Tool
In the Fixed-Point Tool, under System Under
Design (SUD), select the model
ex_mySin as the
system to convert.
Under Range Collection Mode, select Simulation ranges as the method of range collection. This configures the model to collect ranges using idealized floating-point data types.
In the Prepare System section of the toolstrip, click Prepare.
Click the Collect Ranges button to start the simulation
The Fixed-Point Tool stores the simulation data in a run titled
Ranges(Double). Examine the range information of the
MATLAB variables in the spreadsheet.
To launch the code view, in the Convert Data Types section of the toolstrip, click MATLAB Functions.
Select the Function Replacements tab.
Enter the name of the function you want to replace.
For this example, enter
Table, and then click .
The fixed-point conversion process infers the ranges for the function and then uses an interpolated lookup table to replace the function. By default, the lookup table uses linear interpolation, 1000 points, and the minimum and maximum values detected by running the test file.
Click Propose to get data type proposals for the variables.
Click Apply to apply the data type proposals and generate a fixed-point lookup table.
If the behavior of the generated fixed-point code does not match the behavior of the original code closely enough, modify the interpolation method or number of points used in the lookup table and then regenerate the fixed-point code.
Return to the Fixed-Point Tool. In the Verify section of the toolstrip, click the Simulate with Embedded Types button to simulate the model using the newly applied fixed-point data types. The model simulates with the fixed-point variant as the active variant.