Generate C/C++ code from MATLAB code
codegen options files fcn_1 args...
fcn_n args
codegen project_name
codegen
translates the MATLAB® functions options
files
fcn_1
args
...
fcn_n argsfcn_1
through fcn_n
to
a C/C++ static or dynamic library, executable, or MEX function. Optionally,
you can specify custom files
to include
in the build. codegen
applies the options
to
functions fcn_1
through fcn_n
.
It applies args
to the preceding function
only (fcn_n
). If you specify C++, MATLAB Coder™ wraps
the C code into .cpp files so that you can use a C++ compiler and
interface with external C++ applications. It does not generate C++
classes.
codegen
generates
output for the MATLAB Coder project project_name
project_name
. codegen
generates
a MEX function, C/C++ static or dynamic library, or C/C++ executable
depending on the project settings that you defined for project_name
.
By default, codegen
generates files in
the folder codegen/
.target
/fcn_name
target
can be:
mex
for MEX functions
exe
for embeddable C/C++ executables
lib
for embeddable C/C++ static
libraries
dll
for C/C++ dynamic libraries
fcn_name
is the name of
the first MATLAB function (alphabetically) at the command line.
codegen
copies the MEX function and executable
file to the current working folder or to the output folder that the -d
option
specifies.
Each time codegen
generates the same type
of output for the same code or project, it removes the files from
the previous build. If you want to preserve files from a previous
build, before starting another build, copy them to a different location
|
| ||
|
If these MATLAB functions are in files on a path that contains
non 7-bit ASCII characters, such as Japanese characters, it is possible
that If you are using the LCC compiler, do not name an entry-point
function |
|
Space-separated list of custom files to include in generated code. You can include the following types of files:
If these files are on a path that contains non 7-bit ASCII characters,
such as Japanese characters, it is possible that |
|
Choice of compiler options.
| |||||||||||||||||||||||||||||||||||||||||||
|
Name of the MATLAB Coder project that you want |
Generate a MEX function from a MATLAB function that is suitable for code generation.
Write a MATLAB function, coderand
,
that generates a random scalar value from the standard uniform distribution
on the open interval (0,1).
function r = coderand() %#codegen % The directive %#codegen indicates that the function % is intended for code generation r = rand();
Generate and run the MEX function. By default, codegen
names
the generated MEX function coderand_mex
.
codegen coderand
coderand_mex
Generate C executable files from a MATLAB function that is suitable for code generation. Specify the main C function as a configuration parameter.
Write a MATLAB function, coderand
,
that generates a random scalar value from the standard uniform distribution
on the open interval (0,1).
function r = coderand() %#codegen r = rand();
Write a main C function, c:\myfiles\main.c
,
that calls coderand
.
/* ** main.c */ #include <stdio.h> #include <stdlib.h> #include "coderand.h" #include "coderand_initialize.h" #include "coderand_terminate.h" int main() { coderand_initialize(); printf("coderand=%g\n", coderand()); coderand_terminate(); return 0; }
Configure your code generation parameters to include the main C function, then generate the C executable.
cfg = coder.config('exe') cfg.CustomSource = 'main.c' cfg.CustomInclude = 'c:\myfiles' codegen -config cfg coderand
codegen
generates a C executable, coderand.exe
,
in the current folder, and supporting files in the default folder, codegen/exe/coderand
.
This example shows how to specify a main function as a parameter
in the configuration object coder.CodeConfig
. Alternatively,
you can specify the file containing main()
separately
at the command line. You can use a source, object, or library file.
Generate C library files in a custom folder from a MATLAB function with inputs of different classes and sizes. The first input is a 1-by-4 vector of unsigned 16-bit integers. The second input is a double-precision scalar.
Write a MATLAB function, mcadd
,
that returns the sum of two values.
function y = mcadd(u,v) %#codegen y = u + v;
Generate the C library files in a custom folder mcaddlib
using
the -config:lib
option.
codegen -d mcaddlib -config:lib mcadd -args {zeros(1,4,'uint16'),0}
Generate C library files from a MATLAB function that takes a fixed-point input.
Write a MATLAB language function, mcsqrtfi
,
that computes the square root of a fixed-point input.
function y = mcsqrtfi(x) %#codegen y = sqrt(x);
Define numerictype
and fimath
properties
for the fixed-point input x
and generate C library
code for mcsqrtfi
using the -config:lib
option.
T = numerictype('WordLength',32, ... 'FractionLength',23, ... 'Signed',true) F = fimath('SumMode','SpecifyPrecision', ... 'SumWordLength',32, ... 'SumFractionLength',23, ... 'ProductMode','SpecifyPrecision', ... 'ProductWordLength',32, ... 'ProductFractionLength',23) % Define a fixed-point variable with these % numerictype and fimath properties myfiprops = {fi(4.0,T,F)} codegen -config:lib mcsqrtfi -args myfiprops
codegen
generates
C library and supporting files in the default folder, codegen/lib/mcsqrtfi
.Specify global data at the command line.
Write a MATLAB function, use_globals
,
that takes one input parameter u
and uses two global
variables AR
and B
.
function y = use_globals(u) %#codegen % Turn off inlining to make % generated code easier to read coder.inline('never'); global AR; global B; AR(1) = u(1) + B(1); y = AR * 2;
Generate a MEX function. By default, codegen
generates
a MEX function named use_globals_mex
in the current
folder. Specify the properties of the global variables at the command
line by using the -globals
option. Specify that
input u
is a real, scalar, double, by using the -args
option.
codegen -globals {'AR', ones(4), 'B', [1 2 3 4]} ... use_globals -args {0}
Alternatively, you can initialize the global data in the MATLAB workspace. At the MATLAB prompt, enter:
global AR B;
AR = ones(4);
B = [1 2 3];
use_globalsx
.codegen use_globals -args {0}
Generate output for a MATLAB Coder project, test_foo.prj
,
that includes one file, foo.m
, and has it output
type set to C/C++ Static Library.
codegen test_foo.prj
codegen
generates a C library, foo
,
in the codegen\lib\foo
folder.
Generate a MEX function for a function, displayState
,
that has an input parameter that is an enumerated type.
Write a function, displayState
,
that uses enumerated data to activate an LED display, based on the
state of a device. It lights a green LED display to indicate the ON
state. It lights a red LED display to indicate the OFF state.
function led = displayState(state) %#codegen if state == sysMode.ON led = LEDcolor.GREEN; else led = LEDcolor.RED; end
Define an enumeration LEDColor
.
On the MATLAB path, create a file named 'LEDColor' containing:
classdef LEDcolor < int32 enumeration GREEN(1), RED(2), end end
Create a coder.EnumType
object using
a value from an existing MATLAB enumeration.
Define an enumeration sysMode
.
On the MATLAB path, create a file named 'sysMode' containing:
classdef sysMode < int32 enumeration OFF(0) ON(1) end end
Create a coder.EnumType
object from
this enumeration.
t = coder.typeof(sysMode.OFF);
Generate a MEX function for displayState
.
codegen displayState -args {t}
Convert floating-point MATLAB code to fixed-point C code
This example requires a Fixed-Point Designer license.
Write a MATLAB function, myadd
,
that returns the sum of two values.
function y = myadd(u,v) %#codegen y = u + v; end
Write a MATLAB function, myadd_test
,
to test myadd
.
function y = myadd_test %#codegen y = myadd(10,20); end
Create a coder.FixptConfig
object, fixptcfg
,
with default settings.
fixptcfg = coder.config('fixpt');
Set the test bench name.
fixptcfg.TestBenchName = 'myadd_test';
Create a code generation configuration object to generate a standalone C static library.
cfg = coder.config('lib');
Generate the code using the -float2fixed
option.
codegen -float2fixed fixptcfg -config cfg myadd
Convert double-precision MATLAB code to single-precision C code.
This example requires a Fixed-Point Designer license.
Suppose that myfunction
takes two double
scalar inputs. Use the -singleC
option to generate
single-precision C/C++ code.
codegen -singleC myfunction -args {1 2}
Use the coder
function
to open the MATLAB Coder app and create a MATLAB Coder project.
The app provides a user interface that facilitates adding MATLAB files,
defining input parameters, and specifying build parameters.
coder
| coder.EnumType
| coder.FixptConfig
| coder.runTest
| coder.typeof
| fi
| fimath
| mex
| numerictype
| parfor