Main Content

Convert MATLAB Code to Fixed-Point C Code

This example shows how to generate fixed-point C code from floating-point MATLAB® code using the programmatic workflow.

Set Up the Fixed-Point Configuration Object

Create a fixed-point configuration object and configure the test file name. For example:

fixptcfg = coder.config('fixpt');
fixptcfg.TestBenchName = 'fun_with_matlab_test';

Configure the Fixed-Point Configuration Object for Type Proposal

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:

    1. Specify the design range for input parameters. For example:

      fixptcfg.addDesignRangeSpecification('dti', 'u_in', -1.0, 1.0);
      
    2. Enable derived range analysis. Disable collection and reporting of simulation range data.

      fixptcfg.ComputeDerivedRanges = true;
      fixptcfg.ComputeSimulationRanges = false;
      

Enable Numerics Testing

Select to run the test file to verify the generated fixed-point MATLAB code.

fixptcfg.TestNumerics = true;

Enable Plotting

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

Configure additional fixed-point configuration object properties as necessary. For example, define the default fixed-point word length:

fixptcfg.DefaultWordLength = 16;

Set Up the C Code Generation Configuration Object

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;

Generate Fixed-Point C Code

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

View the Type Proposal Report

Click the link to the type proposal report for the entry-point function.

View the Comparison Plots

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.

View the Generated Fixed-Point MATLAB and Fixed-Point C Code

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.

See Also

Related Examples

More About