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 a local working folder, for example,
Change to the
At the MATLAB command line, enter:
cd(fullfile(docroot, 'toolbox', 'fixpoint', 'examples'))
to your local working folder.
|Function code||Entry-point MATLAB function|
MATLAB script that tests
Navigate to the work folder that contains the file for this example.
On the MATLAB Toolstrip Apps tab, under Code Generation, click the app icon.
To add the entry-point function
the project, browse to the file
and then click Open. By default, the app saves
information and settings for this project in the current folder in
a file named
Set Numeric Conversion to
to single precision.
Click Next to go to the Define Input Types step.
The app screens
code violations and code generation readiness
issues. The app does not find issues in
On the Define Input Types page,
ex_2ndOrder_filter_test as a test file,
ex_2ndOrder_filter_test. Click Open.
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
Click Next to go to the Check for Run-Time Issues step.
To detect and fix single-precision conversion issues, perform the Check for Run-Time Issues step.
On the Check for Run-Time Issues page,
the app populates the test file field with
the test file that you used to define the input types.
Click Check for Issues.
The app generates a single-precision MEX function from
It runs the test file
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.
Click Next to go to the Generate Code page.
In the Generate dialog box,
set Build type to
Set Language to C.
For other settings, use the default values.
To generate the code, click Generate.
Coder builds the project and generates a C static
library and supporting files in the default subfolder,
The app displays the generated code for
Double-precision variables have type
the C code.
i is an integer.
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.