What Is C/C++ Code Generation from MATLAB?
You can use Audio 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 Get 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
(MATLAB Coder) 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
(MATLAB Coder) 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
(MATLAB Coder) 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
(MATLAB Coder) 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 Generating Standalone C/C++ Executables from MATLAB Code (MATLAB Coder) for more information.
C/C++ Compiler Setup
Before using codegen
(MATLAB Coder) 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
(MATLAB Coder) function to compile your
code.
Functions and System Objects That Support Code Generation
All Audio Toolbox functions and System objects support code generation.