Main Content

Generate MEX Functions by Using the MATLAB Coder App

Workflow for Generating MEX Functions Using the MATLAB Coder App


Set up the MATLAB® Coder™ project.

Set Up a MATLAB Coder Project

Specify the build configuration parameters. Set Build type to MEX.

Configure Project Settings

Build the project.

Build a MATLAB Coder Project

The MATLAB Coder app is not supported in MATLAB Online™. To generate MEX functions in MATLAB Online, use the codegen command.

Generate a MEX Function Using the MATLAB Coder App

This example shows how to generate a MEX function from MATLAB code using the MATLAB Coder app.

Create the Entry-Point Function

In a local writable folder, create a MATLAB file, mcadd.m, that contains:

function y = mcadd(u,v) %#codegen
y = u + v;

Create the Test File

In the same local writable folder, create a MATLAB file, mcadd_test.m, that calls mcadd with example inputs. The example inputs are scalars with type int16.

function y = mcadd_test
y = mcadd(int16(2), int16(3));

Open the MATLAB Coder App

On the MATLAB Toolstrip Apps tab, under Code Generation, click the MATLAB Coder app icon.

The app opens the Select Source Files page.

Specify Source Files

  1. On the Select Source Files page, type or select the name of the entry-point function mcadd.

    The app creates a project with the default name mcadd.prj.

  2. 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.

Define Input Types

Because C uses static typing, at compile time, MATLAB Coder must determine the properties of all variables in the MATLAB files. You must specify the properties of all entry-point function inputs. From the properties of the entry-point function inputs, MATLAB Coder can infer the properties of all variables in the MATLAB files.

Specify the test file mcadd_test.m that MATLAB Coder uses to automatically define types for u and v:

  1. Enter or select the test file mcadd_test.m.

  2. Click Autodefine Input Types.

    The test file, mcadd_test.m, calls the entry-point function, mcadd, with the example input types. MATLAB Coder infers that inputs u and v are int16(1x1).

  3. Click Next to go to the Check for Run-Time Issues step.

Check for Run-Time Issues

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. You can detect and fix run-time errors that are harder to diagnose in the generated C code.

  1. To open the Check for Run-Time Issues dialog box, click the Check for Issues arrow .

    The app populates the test file field with mcadd_test, the test file that you used to define the input types.

  2. Click Check for Issues.

    The app generates a MEX function. It runs the test file replacing calls to mcadd with calls to the MEX function. 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.

  3. Click Next to go to the Generate Code step.

Generate the MEX Function

  1. To open the Generate dialog box, click the Generate arrow .

  2. In the Generate dialog box, set Build type to MEX and Language to C. Use the default values for the other project build configuration settings.

  3. Click Generate.

    The app indicates that code generation succeeded. It displays the source MATLAB files and the generated output files on the left side of the page. On the Variables tab, it displays information about the MATLAB source variables. On the Target Build Log tab, it displays the build log, including compiler warnings and errors.

    MATLAB Coder builds the project and, by default, generates a MEX function, mcadd_mex, in the current folder. MATLAB Coder also generates other supporting files in a subfolder called codegen/mex/mcadd. MATLAB Coder uses the name of the MATLAB function as the root name for the generated files. It creates a platform-specific extension for the MEX file. See Naming Conventions.

  4. To view the code generation report, click View Report.

  5. Click Next to open the Finish Workflow page.

Review the Finish Workflow Page

The Finish Workflow page indicates that code generation succeeded. It provides a project summary and links to the generated output.

Configure Project Settings

To open the project settings dialog box:

  1. To open the Generate dialog box, click the Generate arrow .

  2. Click More Settings.

To change a project setting, click the tab that contains the setting that you want to change. For example, to change the Saturate on integer overflow setting, click the Speed tab.

MEX functions use a different set of configuration parameters than libraries and executables. When you change the output type from MEX Function to Source Code Static Library, Dynamic Library, or Executable, verify these settings.

Certain configuration parameters are relevant for both MEX and standalone code generation. If you enable any of these parameters when the output type is MEX Function, and you want to use the same setting for C/C++ code generation as well, you must enable it again for C/C++ Static Library, C/C++ Dynamic Library, and C/C++ Executable.

See Also

Build a MATLAB Coder Project

To build a project using the specified settings, on the Generate Code page, click Generate. As the MATLAB Coder app builds a project, it displays the build progress. When the build is complete, the app provides details about the build on the Target Build Log tab.

If the code generation report is enabled or build errors occur, the app generates a report. The report provides detailed information about the most recent build, and provides a link to the report.

To view the report, click the View report link. The report provides links to your MATLAB code and generated C/C++ files and compile-time type information for the variables in your MATLAB code. If build errors occur, the report lists errors and warnings.

See Also

Related Topics