Convert MATLAB Code to Fixed-Point C Code - MATLAB & Simulink (original) (raw)

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.

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.

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

coder.FixptConfig

Topics