Skip to main content
v2026.11,858 entries · CC-BY 4.0

MATLAB Coder: Generate C/C++ Code and MEX Functions from MATLAB

How to use MATLAB Coder: generate a MEX function first, then C/C++ libraries or executables with codegen, with input-type rules and fixes.

Written and maintained by CASRAI Editorial Board

Last updated

MATLAB Coder generates C or C++ code from MATLAB code. Per MathWorks (product page, read 2026-11-08), the output can be source code, static libraries or dynamic libraries for targets from desktop systems to embedded hardware, or a MEX function that you call from inside MATLAB. This guide gives a researcher or analyst one path: build a MEX function first to check the code, then move to a library or executable, with the rules that decide which input types and settings win.

What MATLAB Coder is used for in research

MathWorks describes MATLAB Coder as generating C/C++ code from MATLAB code, usable on any CPU including Intel and ARM Cortex, with Raspberry Pi supported through a support package, and with any C/C++ compiler. Typical research uses follow from the output types:

  • Speed inside MATLAB: a MEX function is a compiled version of your function that MATLAB calls like any other function.
  • Reuse outside MATLAB: a static or dynamic library, or an executable, lets collaborators or instruments run an algorithm without a MATLAB session.
  • C++ classes: MathWorks states Coder can produce C++ classes from value classes, handle classes and System objects.

It is a separate product from MATLAB itself. For what MATLAB covers, see what MATLAB is used for and MATLAB and Simulink.

MATLAB Coder, the Coder app and the codegen command

The searches “matlab matlab coder” and “MATLAB Coder toolbox” refer to the same thing: MathWorks documents both an app and command-line functions. The app walks you through steps; the codegen command does the same job from a script, which is easier to repeat and to record in a methods section.

Install or access

MATLAB Coder is obtained from MathWorks. Use the official product page at mathworks.com/products/matlab-coder and your institution’s MathWorks account; see the CASRAI guides on MATLAB installation and MATLAB licensing for how access is normally arranged. The Coder app is not supported in MATLAB Online (MathWorks documentation, read 2026-11-08), so searches for “MATLAB Coder online” lead to a desktop install, not a browser session.

Step by step: from a MATLAB function to generated code

Commands below follow the codegen reference page (read 2026-11-08). Your function must be written in the code-generation subset of MATLAB; the first build will tell you where it is not.

  1. Put the algorithm in a function file. Generate code for a function (the entry point), not a script. Example: foo.m taking one numeric input.
  2. Build a MEX function first. By default the code generator produces a MEX function: codegen helloWorld produces helloWorld_mex.
  3. Define the input types with -args. Pass a cell array of example values or coder.Type objects, for example codegen foo -args {0}. Use coder.typeof when an input can vary in size. You may omit -args when inputs are defined through arguments blocks or assert statements.
  4. Run the MEX function and compare with the original. Call foo_mex with the same inputs as foo and confirm the results match before going further.
  5. Choose the real output type. Use -config:lib for a static library, -config:dll for a dynamic library, -config:exe for an executable, or -config:mex (the default). Add -c to generate source code only.
  6. Pick the language. -lang:c is the default; use -lang:c++ for C++.
  7. Or use a configuration object. cfg = coder.config("lib"); codegen -config cfg foo. If a command-line option and the configuration object disagree, the codegen option takes precedence.

Using the Coder app (the MATLAB Coder tutorial path)

The app’s documented stages are Select Source Files, Define Input Types, Check for Run-Time Issues, then generate. Check for Run-Time Issues is optional but recommended: it builds a MEX function and runs your test file. Inferring input types from a test script is the documented best practice; you can also set them manually.

Which way to define inputs

Situation Use Source
Fixed-size numeric input Example value in -args, e.g. {0} codegen reference
Input whose size varies coder.typeof inside -args codegen reference
Types already declared in the function arguments block or assert; -args may be omitted codegen reference
Many inputs, using the app Infer types from a test script Coder app documentation

Output and how to read it

  • MEX build: a file named <function>_mex that you call from MATLAB.
  • Library or executable builds: generated C/C++ files and the library or executable chosen with -config.
  • Agreement test: the generated code is only trustworthy for your study once its outputs match the MATLAB function on representative inputs, which is why step 4 comes before step 5.

Troubleshooting

Symptom What to check
Build fails in the app The Errors tab lists build errors (Check for Run-Time Issues stage)
Build works, run fails The Test Output tab shows run-time errors from the MEX run
Wrong input type or size error Revisit -args; use coder.typeof for variable-size inputs
Output type is not what you wanted A command-line -config option overrides the configuration object
App will not open in a browser The Coder app is not supported in MATLAB Online; use desktop MATLAB or the codegen command

Free alternatives

These are general-purpose routes to faster numerical code; none is a drop-in replacement for generating C/C++ from existing MATLAB code. For a language-level comparison see MATLAB vs R.

Tool Relation to the task
GNU Octave Free environment that runs many MATLAB-style scripts; a separate product from MATLAB Coder
Julia Free language with a compiler aimed at numerical work; requires rewriting the code
Python with Numba Just-in-time compiles numerical Python functions; requires porting from MATLAB
Cython Compiles Python-style code to C; requires porting from MATLAB

FAQ

Is there a MATLAB Coder Onramp or tutorial?

MathWorks offers training for MATLAB Coder through its training pages. The step-by-step section above follows the codegen reference.

Can I use MATLAB Coder in MATLAB Online?

The Coder app is not supported in MATLAB Online, per MathWorks documentation read 2026-11-08.

Does MATLAB Coder produce a MEX function by default?

Yes. By default the code generator produces a MEX function, for example helloWorld_mex from codegen helloWorld.

Is there a MATLAB code generator that uses AI?

That is a different question: MATLAB Coder converts existing MATLAB code to C/C++ and is not an AI code writer.

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about MATLAB Coder: Generate C/C++ Code and MEX Functions from MATLAB

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →