Main Content

Generate Single-Precision C Code Using the MATLAB Coder App

This example shows how to generate single-precision C code from double-precision MATLAB® code by using the MATLAB Coder™ app.

Prerequisites

To complete this example, install the following products:

Create a Folder and Copy Relevant Files

  1. Create a local working folder, for example, c:\ex_2ndOrder_filter.

  2. Change to the docroot\toolbox\fixpoint\examples folder. At the MATLAB command line, enter:

    cd(fullfile(docroot, 'toolbox', 'fixpoint', 'examples')) 

  3. Copy the ex_2ndOrder_filter.m and ex_2ndOrder_filter_test.m files to your local working folder.

    TypeNameDescription
    Function codeex_2ndOrder_filter.mEntry-point MATLAB function
    Test fileex_2ndOrder_filter_test.m

    MATLAB script that tests ex_2ndOrder_filter.m

     The ex_2ndOrder_filter Function

     The ex_2ndOrder_filter_test Script

Open the MATLAB Coder App

  1. Navigate to the work folder that contains the file for this example.

  2. On the MATLAB Toolstrip Apps tab, under Code Generation, click the app icon.

Select the Source Files

To add the entry-point function ex_2ndOrder_filter to the project, browse to the file ex_2ndOrder_filter.m, and then click Open. By default, the app saves information and settings for this project in the current folder in a file named ex_2ndOrder_filter.prj.

Enable Single-Precision Conversion

  1. Set Numeric Conversion to Convert to single precision.

  2. Click Next to go to the Define Input Types step.

    The app screens ex_2ndOrder_filter.m for code violations and code generation readiness issues. The app does not find issues in ex_2ndOrder_filter.m.

Define Input Types

  1. On the Define Input Types page, to add ex_2ndOrder_filter_test as a test file, browse to ex_2ndOrder_filter_test. Click Open.

  2. Click Autodefine Input Types.

    The test file runs and displays the outputs of the filter for each of the input signals. The app determines that the input type of x is double(1x256).

  3. Click Next to go to the Check for Run-Time Issues step.

Check for Run-Time Issues

To detect and fix single-precision conversion issues, perform the Check for Run-Time Issues step.

  1. On the Check for Run-Time Issues page, the app populates the test file field with ex_2ndOrder_filter_test, the test file that you used to define the input types.

  2. Click Check for Issues.

    The app generates a single-precision MEX function from ex_2ndOrder_filter. It runs the test file ex_2ndOrder_filter_test replacing calls to ex_2ndOrder_filter with calls to the generated MEX function. If the app finds issues, it provides warning and error messages. Click a message to highlight the problematic code in a window where you can edit the code. In this example, the app does not detect issues.

  3. Click Next to go to the Generate Code page.

Generate Single-Precision C Code

  1. In the Generate dialog box, set Build type to Static Library.

  2. Set Language to C.

  3. For other settings, use the default values.

  4. To generate the code, click Generate.

    MATLAB Coder builds the project and generates a C static library and supporting files in the default subfolder, codegen/lib/ex_2ndOrder_filter.

View the Generated C Code

The app displays the generated code for ex_2ndOrder_filter.c.

  • Double-precision variables have type float in the C code.

  • The index i is an integer.

View Potential Data Type Issues

When you generate single-precision code, the app enables highlighting of potential data type issues in the code generation report. If the app cannot remove a double-precision operation, the report highlights the MATLAB expression that results in the operation.

To open the code generation report, click the View Report link.

Click the Code Insights tab. Expand Potential data type issues. The absence of double-precision operations indicates that no double-precision operations remain.

Related Examples

More About