Main Content

Generate MEX Functions at the Command Line

Command-line Workflow for Generating MEX Functions


Install prerequisite products.

Installing Prerequisite Products

Set up your file infrastructure.

Paths and File Infrastructure Setup

Fix errors detected by the code analyzer.

Fixing Errors Detected at Design Time

Specify build configuration parameters.

Specify Build Configuration Parameters

Specify properties of primary function inputs.

Specify Properties of Entry-Point Function Inputs

Generate the MEX function using codegen with suitable command-line options.


Generate a MEX Function at the Command Line

In this example, you use the codegen function to generate a MEX function from a MATLAB® file that adds two inputs. You use the codegen -args option to specify that both inputs are int16.

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

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

  2. Generate a platform-specific MEX function in the current folder. At the command line, specify that the two input parameters are int16 using the -args option. By default, if you do not use the -args option, codegen treats inputs as real, scalar doubles.

    codegen mcadd -args {int16(0), int16(0)}

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

Related Topics