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 a Folder and Copy Relevant Files
Create a local working folder, for example,
Change to the
docroot\toolbox\fixpoint\examplesfolder. At the MATLAB command line, enter:
cd(fullfile(docroot, 'toolbox', 'fixpoint', 'examples'))
ex_2ndOrder_filter_test.mfiles to your local working folder.
Type Name Description Function code
Entry-point MATLAB function Test file
MATLAB script that tests
Open the MATLAB Coder App
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.
Select the Source Files
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
Enable Single-Precision Conversion
Set Numeric Conversion to
Convert to single precision.
Click Next to go to the Define Input Types step.
The app screens
ex_2ndOrder_filter.mfor code violations and code generation readiness issues. The app does not find issues in
Define Input Types
On the Define Input Types page, to add
ex_2ndOrder_filter_testas a test file, browse to
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 of
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.
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.
Click Check for Issues.
The app generates a single-precision MEX function from
ex_2ndOrder_filter. It runs the test file
ex_2ndOrder_filter_testreplacing calls to
ex_2ndOrder_filterwith 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.
Generate Single-Precision C Code
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.
MATLAB Coder builds the project and generates a C static library and supporting files in the default subfolder,
View the Generated C Code
The app displays the generated code for
Double-precision variables have type
floatin the C code.
iis 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.