MATLAB Coder - Generate C code or MEX function from MATLAB code - MATLAB (original) (raw)
Generate C code or MEX function from MATLAB code
Description
The MATLAB Coder app generates C and C++ code from MATLAB® code as standalone static libraries, dynamically linked libraries, or executables. The generated code can be embedded in applications outside the MATLAB environment. The MATLAB Coder™ app also generates C/C++ MATLAB executable (MEX) functions designed for MATLAB acceleration. The MATLAB Coder app is integrated into the MATLAB desktop and is available in MATLAB Online™.
The user interface includes:
- The MATLAB Coder tab on the toolstrip, where you can configure code generation settings, control the language and build type of the generated code, open the Entry Points pane, generate code, and perform code verification.
- The Entry Points pane, where you can add entry-point functions and global variables. You can also define input types automatically or manually.
- The MATLAB Coder panel, which suggests next steps. In this panel, the Input section summarizes entry points and global variable inputs, and the Output section summarizes the output. After you generate code, this section provides quick access to the generated code.
Using the app, you can:
- Generate code in MATLAB Online.
- Create a project or open an existing project. The project specifies the input files, entry-point function input types, and build configuration.
- Review code generation readiness issues, including unsupported functions.
- Check the generated MEX function for run-time issues.
- Switch between the MATLAB editor and the code generation Entry Points pane.
- Verify the numerical behavior of generated code using software-in-the-loop and processor-in-the-loop execution (requires Embedded Coder®).
- Export project settings in the form of a MATLAB script.
- Access generated files.
- Package generated files as a single zip file for deployment outside of MATLAB.
When the app creates a project, the app enables Embedded Coder features if the Embedded Coder product is installed. When Embedded Coder features are enabled, code generation requires an Embedded Coder license. To disable Embedded Coder features, open the MEX Code Generation Settings or Standalone Code Generation Settings dialog box. On the Advanced pane, clear theUse Embedded Coder features check box.
Open the MATLAB Coder App
- MATLAB Toolstrip: On the Apps tab, underCode Generation, click the app icon.
- MATLAB command prompt: Enter
matlabcoder
.
Programmatic Use
Version History
Introduced in R2015a
The MATLAB Coder app has a new look and feel and is integrated into the MATLAB desktop.
You can use the MATLAB Coder app in MATLAB Online.
The MATLAB Coder app saves code generation project files with a.coderprj
extension, instead of with a.prj
extension. If you open a MATLAB Coder project file created before R2025a in the new app, the app saves a copy of the PRJ file to a CODERPRJ file. If you save changes to your project, the changes are saved to the CODERPRJ file, not the PRJ file. In some cases, the app is unable to migrate parameters that are specific to a hardware support package from the PRJ file to the CODERPRJ file. If you use a hardware support package, verify that the configuration parameters are correct before generating code.
To check for issues in the generated MEX file, use the Run Generated MEX button in the MATLAB Coder toolstrip. To perform verification using MEX, you must provide aMATLAB Coder script that exercises your entry points (also known as a_test bench_). In previous releases, you verified the generated code by using the Check for Issues step in theMATLAB Coder app.
After you verify the generated code by using Run Generated MEX, you can view line execution counts and other execution profile data by using the MATLAB Profiler. See Profile MEX Functions by Using MATLAB Profiler. Before R2025a, line execution counts were available in the MATLAB Coder app after completing the Check for Issues step.
The MATLAB Coder app no longer supports numeric conversion. Perform numeric conversion at the command line by using the codegen command.
- To convert floating-point MATLAB code to fixed-point C/C++ code, use the
codegen
command with the-float2fixed
option. See Convert MATLAB Code to Fixed-Point C Code. This conversion requires Fixed-Point Designer™. - To convert double-precision MATLAB code to single-precision C/C++ code, use the
codegen
command with the-singleC
option. See Generate Single-Precision C Code at the Command Line. This conversion requires Fixed-Point Designer.
The Search paths parameter is not available when you generate code by using the MATLAB Coder app. To add additional folders to the search path, generate code at the command line by using the codegen command with the-I
option.