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.


To complete this example, install the following products:

Create Code Files

  1. In a local, writable folder, create a function ex_2ndOrder_filter.m.

    function y = ex_2ndOrder_filter(x) %#codegen
      persistent z
      if isempty(z)
          z = zeros(2,1);
      % [b,a] = butter(2, 0.25)
      b = [0.0976310729378175,  0.195262145875635,  0.0976310729378175];
      a = [1, -0.942809041582063,  0.3333333333333333];
      y = zeros(size(x));
      for i = 1:length(x)
          y(i) = b(1)*x(i) + z(1);
          z(1) = b(2)*x(i) + z(2) - a(2) * y(i);
          z(2) = b(3)*x(i)        - a(3) * y(i);
  2. Create a test file, ex_2ndOrder_filter_test.m, to exercise the ex_2ndOrder_filter algorithm.

    It is a best practice to create a separate test script for preprocessing and postprocessing such as:

    • Setting up input values.

    • Calling the function under test.

    • Outputting the test results.

    To cover the full intended operating range of the system, the test script runs the ex_2ndOrder_filter function with three input signals: chirp, step, and impulse. The script then plots the outputs.

    % ex_2ndOrder_filter_test
    % Define representative inputs
    N = 256;                   % Number of points
    t = linspace(0,1,N);       % Time vector from 0 to 1 second
    f1 = N/2;                  % Target frequency of chirp set to Nyquist
    x_chirp = sin(pi*f1*t.^2); % Linear chirp from 0 to Fs/2 Hz in 1 second
    x_step = ones(1,N);        % Step
    x_impulse = zeros(1,N);    % Impulse
    x_impulse(1) = 1;
    % Run the function under test
    x = [x_chirp;x_step;x_impulse];
    y = zeros(size(x));
    for i = 1:size(x,1)
      y(i,:) = ex_2ndOrder_filter(x(i,:));
    % Plot the results
    titles = {'Chirp','Step','Impulse'}
    for i = 1:size(x,1)
    xlabel('Time (s)')
    disp('Test complete.')

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