This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

What is C Code Generation from MATLAB?

You can use WLAN Toolbox™ together with MATLAB® Coder™ to:

  • Create a MEX file to speed up your MATLAB application.

  • Generate ANSI®/ISO® compliant C/C++ source code that implements your MATLAB functions and models.

  • Generate a standalone executable that runs independently of MATLAB on your computer or another platform.

In general, the code you generate using the toolbox is portable ANSI C code. In order to use code generation, you need a MATLAB Coder license. For more information, see Getting Started with MATLAB Coder (MATLAB Coder).

Using MATLAB Coder

Creating a MATLAB Coder MEX file can substantially accelerate your MATLAB code. It is also a convenient first step in a workflow that ultimately leads to completely standalone code. When you create a MEX file, it runs in the MATLAB environment. Its inputs and outputs are available for inspection just like any other MATLAB variable. You can then use MATLAB tools for visualization, verification, and analysis.

The simplest way to generate MEX files from your MATLAB code is by using the codegen function at the command line. For example, if you have an existing function, myfunction.m, you can type the commands at the command line to compile and run the MEX function. codegen adds a platform-specific extension to this name. In this case, the "mex" suffix is added.

codegen myfunction.m
myfunction_mex;

Within your code, you can run specific commands either as generated C code or by using the MATLAB engine. In cases where an isolated command does not yet have code generation support, you can use the coder.extrinsic command to embed the command in your code. This means that the generated code reenters the MATLAB environment when it needs to run that particular command. This is also useful if you want to embed commands that cannot generate code (such as plotting functions).

To generate standalone executables that run independently of the MATLAB environment, create a MATLAB Coder project inside the MATLAB Coder Integrated Development Environment (IDE). Alternatively, you can call the codegen command in the command line environment with appropriate configuration parameters. A standalone executable requires you to write your own main.c or main.cpp function. See C/C++ Code Generation (MATLAB Coder) for more information.

C/C++ Compiler Setup

Before using codegen to compile your code, you must set up your C/C++ compiler. For 32-bit Windows platforms, MathWorks® supplies a default compiler with MATLAB. If your installation does not include a default compiler, you can supply your own compiler. For the current list of supported compilers, see Supported and Compatible Compilers on the MathWorks website. Install a compiler that is suitable for your platform, then read Setting Up the C or C++ Compiler (MATLAB Coder). After installation, at the MATLAB command prompt, run mex -setup. You can then use the codegen function to compile your code.

Functions and System Objects That Support Code Generation

All WLAN Toolbox functions and System objects support for code generation. For an overview, see Functions and System Objects Supported for MATLAB Coder.

See Also

Functions

Related Topics