This example shows how to generate fixed-point C code from floating-point MATLAB® code using the programmatic workflow.
Configure the Fixed-Point Configuration Object for Type Proposal
Configure Additional Fixed-Point Configuration Object Properties
View the Generated Fixed-Point MATLAB and Fixed-Point C Code
Create a fixed-point configuration object and configure the test file name. For example:
fixptcfg = coder.config('fixpt'); fixptcfg.TestBenchName = 'fun_with_matlab_test';
The fixed-point conversion software can propose types based on simulation ranges, derived ranges, or both.
For type proposal using only simulation ranges, enable the collection and reporting of simulation range data. By default, derived range analysis is disabled.
fixptcfg.ComputeSimulationRanges = true;
For type proposal using only derived ranges:
Specify the design range for input parameters. For example:
fixptcfg.addDesignRangeSpecification('dti', 'u_in', -1.0, 1.0);
Enable derived range analysis. Disable collection and reporting of simulation range data.
fixptcfg.ComputeDerivedRanges = true; fixptcfg.ComputeSimulationRanges = false;
Select to run the test file to verify the generated fixed-point MATLAB code.
fixptcfg.TestNumerics = true;
Log inputs and outputs for comparison plotting. Select to plot using a custom function or Simulation Data Inspector. For example, to plot using Simulation Data Inspector:
fixptcfg.LogIOForComparisonPlotting = true; fixptcfg.PlotWithSimulationDataInspector = true;
Configure additional fixed-point configuration object properties as necessary. For example, define the default fixed-point word length:
fixptcfg.DefaultWordLength = 16;
Create a code configuration object for generation of a C static library, dynamic library, or executable. Enable the code generation report. For example:
cfg = coder.config('lib');
cfg.GenerateReport = true;
Use the codegen
function to convert the
floating-point MATLAB function to fixed-point C code.
For example:
codegen -float2fixed fixptcfg -config cfg fun_with_matlab
Click the link to the type proposal report for the entry-point function.
If you selected to log inputs and outputs for comparison plots, the conversion process generates comparison plots.
If you selected to use Simulation Data Inspector for these plots, the Simulation Data Inspector opens. Use Simulation Data Inspector to view and compare the floating-point and fixed-point run information.
If you selected to use a custom plotting function for these plots, the conversion process uses the custom function to generate the plots.
Click the View Report link that follows the type proposal report. To view the fixed-point MATLAB code, select the function in the MATLAB Source pane. To view the fixed-point C code, select the file in the Generated Code pane.