This tutorial shows you how to:
Generate code for
Generate C code from your MATLAB® code using the MATLAB Coder™ app.
Configure code generation configuration parameters in the MATLAB Coder project.
Generate a code generation report that you can use to trace between the original MATLAB code and the generated C code.
This tutorial requires the following products:
|Function code||MATLAB example that uses |
Create a local working folder, for example,
Change to the
At the MATLAB command prompt, enter:
cd(fullfile(docroot, 'toolbox', 'ecoder', 'examples'))
Copy the file
your local working folder.
On the MATLAB Toolstrip Apps tab, under Code Generation, click the MATLAB Coder app icon.
The app opens the Select Source Files page.
On the Select Source Files page,
type or select the name of the entry-point function
In the Project location field,
change the project name to
Click Next to go to the Define Input Types step. The app analyzes the function for coding issues and code generation readiness. If the app identifies issues, it opens the Review Code Generation Readiness page where you can review and fix issues. In this example, because the app does not detect issues, it opens the Define Input Types page.
Because C uses static typing, at compile time, the
code generator must determine the properties of all
variables in the MATLAB files. Therefore, you must specify the
properties of all function inputs.
To define the properties of the input
Click Let me enter input or global types directly.
Click the field to the right of
From the list of options, select
Click Next to go to the Check for Run-Time Issues step.
Convert if-elseif-else patterns to switch-case
statements optimization works only for integer and enumerated
The Check for Run-Time Issues step generates a MEX file from your entry-point functions, runs the MEX function, and reports issues. This step is optional. However, it is a best practice to perform this step. Using this step, you can detect and fix run-time errors that are harder to diagnose in the generated C code. By default, the MEX function includes memory integrity checks. These checks perform array bounds and dimension checking. The checks detect violations of memory integrity in code generated for MATLAB functions. For more information, see Control Run-Time Checks.
To open the Check for Run-Time Issues dialog box, click the Check for Issues arrow .
In the Check for Run-Time Issues dialog
box, enter code that calls
an example input. For this example, enter
Click Check for Issues.
The app generates a MEX function. It runs the MEX function with the example input. If the app detects issues during the MEX function generation or execution, it provides warning and error messages. Click these messages to navigate to the problematic code and fix the issue. In this example, the app does not detect issues.
Click Next to go to the Generate Code step.
To open the Generate dialog box, click the Generate arrow .
Set the Build type to
Click More settings and set these settings:
On the Code Appearance tab, select the Convert if-elseif-else patterns to switch-case statements check box.
On the Debugging tab, make sure that Always create a report is selected.
On the All Settings tab, make sure that Enable code traceability is selected.
When code generation is complete, the code generator produces a C static library,
test_code_style.lib, and C code in the
/codegen/lib/test_code_style subfolder. The code generator provides a
link to the report.
To open the code generation report, click the View Report link.
test_code_style function is displayed in the code
To view the MATLAB code and the C code next to each other, click Trace Code.
In the MATLAB code, place your cursor over the statement
if (x ==
The report traces
if (x == 1) to a
Click Next to open the Finish Workflow page.
The Finish Workflow page indicates that code generation succeeded. It provides a project summary and links to the generated output.
To check for run-time issues before code generation, perform the Check for Run-Time Issues step.
To access build configuration settings, on the Generate Code page, open the Generate dialog box, and then click More Settings.